我選擇最近的一個MySQL表的項目有:由MAX(時間),時間選擇最近的MySQL行<= X
SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
ORDER BY most_recent DESC
我的問題是,如果我想限制的最大時間與:
WHERE time <= nnn
該查詢不起作用了。有沒有子查詢的解決方案?
在此先感謝!
我選擇最近的一個MySQL表的項目有:由MAX(時間),時間選擇最近的MySQL行<= X
SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
ORDER BY most_recent DESC
我的問題是,如果我想限制的最大時間與:
WHERE time <= nnn
該查詢不起作用了。有沒有子查詢的解決方案?
在此先感謝!
使用HAVING
子句,像這樣:
SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
HAVING MAX(time) <= nnn
ORDER BY most_recent DESC
你可以用子查詢做到這一點:
select t.userID, max(t.time)
from
(
select userID, time
from tableName
where time <= nnn
) t
group by t.userID
或者乾脆:
select userID, max(time)
from tableName
where time <= nnn
group by userID
Unfortunetely這不起作用。它將返回一個空的結果'用戶ID時間> nnn';我有多行具有相同的用戶ID,我想要最新的時間,但<= nnn – schlimpf