mysql - Group by 也是 order by?

标签 mysql sql sql-order-by

两个表: 主题 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/

相关文章:

php - 如何向 PHP/MySQL 网站上的注册用户发送电子邮件通知

android - 如何为 Qt android 构建 MySQL 插件?

sql - PostgreSQL 选择每个客户每个日期范围的最后一个订单

php - ORDER BY 变量 AFTER 语句

php - 在php中获取数组

mysql - mysql中的子查询将一列用于两个不同的结果

sql - 这两个 MySQL 查询中哪个更有效?

SQL - 连接来自同一个表的行

mysql - 寻找正确的查询

Mysql ORDER BY 与复合 PK