我正在尋找每個顯示「名稱」及其最新「分數」(按照時間順序倒序)的「學生」類型用戶返回一行。mySql INNER JOIN,MAX&DISTINCT
我有兩個表的用戶&服務
用戶表
id name type
---|-------|-----
1 | Bob | student
2 | Dave | student
3 | Larry | student
4 | Kevin | master
服務表
id score userId date
---|--------|-------|------------
1 | 14 | 1 | 2014-09-04
2 | 99 | 3 | 2014-09-03
3 | 53 | 2 | 2014-09-07
4 | 21 | 1 | 2014-09-08
5 | 79 | 2 | 2014-09-08
6 | 43 | 3 | 2014-09-10
7 | 72 | 3 | 2014-09-10
8 | 66 | 2 | 2014-09-01
9 | 43 | 3 | 2014-08-22
10 | 26 | 1 | 2014-08-22
所需的結果
id scores name date
---|--------|-------|------------
3 | 43 | Larry | 2014-09-10
1 | 21 | Bob | 2014-09-08
2 | 79 | Dave | 2014-09-08
瓦在我曾嘗試的是:
SELECT users.id, users.name, services.date, services.score
FROM users
JOIN services ON users.id = services.userId
WHERE users.type='student'
ORDER BY services.date DESC
但是,這總是返回每個用戶表中的最後日期。
所以我決定嘗試從另一端是這樣處理的:
SELECT servicesTemp.date, servicesTemp.score
FROM services servicesTemp
INNER JOIN
(SELECT userId, MAX(date) AS MaxExpDate
FROM services
GROUP BY clientId) servicesTempGrp
ON servicesTemp.userId = servicesTempGrp.userId
AND servicesTemp.MaxDate = servicesTempGrp.MaxDate
但意識到,我最終會重複,如果日期是都一樣,我只能返回每一個行用戶(並且雙重分組不起作用)。
我想我現在已經過了複雜化,所以一條生命線會非常感激。
你怎麼43和72之間選擇作爲得分拉里? – Arth 2014-09-24 10:50:25
在這種情況下,只要返回一個,它並不重要。 – 2014-09-24 10:52:28
@TraceyTurn:是的,因爲你的'日期'和'分數'不同步...你是否只是*任何*'分數'返回? – NoobEditor 2014-09-24 10:57:33