3

我正在选择 MySQL 表的最新条目:

SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
ORDER BY most_recent DESC

我的问题是,如果我想限制最长时间:

WHERE time <= nnn

查询不再起作用。有没有子查询的解决方案?

提前致谢!

4

2 回答 2

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
于 2012-11-04T07:08:16.810 回答
0

像这样使用HAVING子句:

SELECT MAX(time) as most_recent, userID
FROM TableName
GROUP BY userID
HAVING MAX(time) <= nnn
ORDER BY most_recent DESC
于 2012-11-04T07:07:55.750 回答