2017-10-17 120 views
0

我有一個預約表,我需要選擇爲所有用戶的最新記錄,目前我的查詢返回的最古老的每個用戶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   | 
+-------+--------------+--------------+--------------+ 
+0

你有一組通過,但沒有聚集函數.. – Randy

回答

1

試試這個

SELECT a.ID, a.time, a.userid, a.description 
FROM users u 
INNER JOIN appointment a ON u.id = a.userid 
WHERE u.status = 1 
AND time in(SELECT MAX(time) from appointment t WHERE t.userid = a.userid) 
+0

作品完美,但我有,如果我一個問題想要添加一個條件爲另一列'季節'在預約會見我應該在哪裏添加'AND'? – mirvatJ

+0

@mirvatJ你應該可以在where子句中的任何地方添加它。你嘗試添加它嗎? – isaace

+0

肯定的,我得到這個錯誤無論我加吧'語法錯誤或訪問衝突:1064您的SQL語法錯誤;檢查對應於你的MySQL服務器版本使用附近的「正確的語法手冊:季節和時間is' – mirvatJ

-1
SELECT max(appointment.id), max(time), user.id, description 
FROM appointment 
LEFT JOIN users AS user 
ON user.id = appointment.userid 
WHERE user.status = 1 
GROUP BY appointment.userid 
+0

這個答案是錯的,而左連接將改造成一個內部聯接,因爲你'WHERE'條款。 – sagi

0

您可以自連接至IDENT IFY的最新任命:

SELECT s.* FROM (
    SELECT * 
    FROM appointment 
    LEFT JOIN users AS user 
    ON user.id = appointment.userid and user.status = 1) s 
LEFT JOIN appointment p 
ON(p.userid = s.userid and p.time < s.time) 
WHERE p.time IS NULL 

雖然我不明白爲什麼要LEFT JOINusers

+0

所以它不返回的情況下,出現了在一個數據庫中的用戶刪除的記錄錯誤,還有什麼做u建議?也sagi爲什麼你用時間爲空? – mirvatJ

+0

什麼?請嘗試解釋你自己。 – sagi

+0

通常當我使用內部連接和我刪除記錄讓來自用戶的說,它在所有的查詢將返回一個錯誤,但是當我使用左側加入它不會(可能是我可能不是正確的),但它只是我的遭遇來我在此之前,我試圖避免 – mirvatJ

0
SELECT MAX(a.ID) ID, MAX(a.time) time, a.userid, MAX(a.description) description 
FROM appointment a 
INNER JOIN users u ON u.id = a.userid 
WHERE u.status = 1 
GROUP BY a.userid 
ORDER BY a.ID 

輸出

ID time     userid description 
1 2017-10-20T00:00:00Z 4  etc 
5 2017-10-24T00:00:00Z 6  etc 
6 2017-10-25T00:00:00Z 7  etc 
4 2017-10-23T00:00:00Z 8  etc 

SQL小提琴:http://sqlfiddle.com/#!9/6b0c1c/7/0

+0

你所依賴的事實'MAX(時間)'將'MAX(ID)來對應' ,儘管在數據樣本中看起來是這樣,但並不一定是這樣。 – sagi

+0

@sagi我同意,我需要更多的樣本數據來確認。 – Matt

+0

但我會給予好評用於察覺它可能:) – sagi

0

我覺得left join與用戶表是不必要的。 下面的查詢將產生預期的結果

select a1.* from appointment a1 
inner join (select max(id) as id ,userid from appointment group by userid) a2 
on a1.id=a2.id 

SQL Fiddle