我有一個預約表,我需要選擇爲所有用戶的最新記錄,目前我的查詢返回的最古老的每個用戶ID,而不是最新的一個每個如何選擇所有的最新記錄所有用戶的
委任表格
+-------+--------------+--------------+--------------+
| ID | time | userid | description |
+-------+--------------+--------------+--------------+
| 1 | 2017-10-20 | 4 | etc |
+-------+--------------+--------------+--------------+
| 2 | 2017-10-21 | 6 | etc |
+-------+--------------+--------------+--------------+
| 3 | 2017-10-22 | 7 | etc |
+-------+--------------+--------------+--------------+
| 4 | 2017-10-23 | 8 | etc |
+-------+--------------+--------------+--------------+
| 5 | 2017-10-24 | 6 | etc |
+-------+--------------+--------------+--------------+
| 6 | 2017-10-25 | 7 | etc |
+-------+--------------+--------------+--------------+
用戶表
+-------+--------------+--------------+--------------+
| ID | first | last | status |
+-------+--------------+--------------+--------------+
| 4 | jo | do | 1 |
+-------+--------------+--------------+--------------+
| 6 | jid | did | 1 |
+-------+--------------+--------------+--------------+
| 7 | jone | done | 1 |
+-------+--------------+--------------+--------------+
| 8 | ja | da | 1 |
+-------+--------------+--------------+--------------+
當前查詢
$sql = "SELECT *
FROM appointment
LEFT JOIN users AS user
ON user.id = appointment.userid
WHERE user.status = 1
GROUP BY appointment.userid
";
當前結果
+-------+--------------+--------------+--------------+
| ID | time | userid | description |
+-------+--------------+--------------+--------------+
| 1 | 2017-10-20 | 4 | etc |
+-------+--------------+--------------+--------------+
| 2 | 2017-10-21 | 6 | etc |
+-------+--------------+--------------+--------------+
| 3 | 2017-10-22 | 7 | etc |
+-------+--------------+--------------+--------------+
| 4 | 2017-10-23 | 8 | etc |
+-------+--------------+--------------+--------------+
預期輸出
+-------+--------------+--------------+--------------+
| ID | time | userid | description |
+-------+--------------+--------------+--------------+
| 1 | 2017-10-20 | 4 | etc |
+-------+--------------+--------------+--------------+
| 4 | 2017-10-23 | 8 | etc |
+-------+--------------+--------------+--------------+
| 5 | 2017-10-24 | 6 | etc |
+-------+--------------+--------------+--------------+
| 6 | 2017-10-25 | 7 | etc |
+-------+--------------+--------------+--------------+
你有一組通過,但沒有聚集函數.. – Randy