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