两张桌子: 话题 评论(有一个主题字段,所以我知道评论在哪个主题中)
我想只获得每个主题中的最新评论(日期字段中的最高值),然后以这种方式排序主题。
我已经尝试过查询:
SELECT User, Topic, Date
FROM Comments
GROUP BY Topic
ORDER BY Date DESC
答案 0 :(得分:3)
如果没有其他具有相同主题的行具有更晚的日期,则返回一行。
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
答案 1 :(得分:1)
试试这个,改进答案:
SELECT `User`,
temp.`Topic`,
temp.`Date`
FROM (
SELECT `Topic`,
MAX(`Date`) `Date`
FROM `Comments`
GROUP BY `Topic`
ORDER BY MAX(`Date`) DESC
) temp
INNER JOIN `Comments`
USING (`Topic`, `Date`)