两个表: 主题 Comments(有一个Topic字段,所以我知道评论是在哪个topic)
我只想获取每个主题中的最新评论(日期字段中的最大值),然后以这种方式对主题进行排序。
我试过的查询:
SELECT User, Topic, Date
FROM Comments
GROUP BY Topic
ORDER BY Date DESC
最佳答案
如果具有相同主题的其他行没有更晚的日期,则返回一行。
SELECT User, Topic, Date
FROM Comments c1
where not exists (select 1 from Comments c2
where c2.topic = c1.topic
and c2.date > c1.date)
order by date desc
关于mysql - Group by 也是 order by?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32719338/