我正在选择 MySQL 表的最新条目:
SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
ORDER BY most_recent DESC
我的问题是,如果我想限制最长时间:
WHERE time <= nnn
查询不再起作用。有没有子查询的解决方案?
提前致谢!
你可以用子查询来做到这一点:
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
像这样使用HAVING
子句:
SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
HAVING MAX(time) <= nnn
ORDER BY most_recent DESC