MySQL 的一列与另一最大列不同
假设我有这个查询:
SELECT report_id, time_sent
FROM report_submissions
WHERE report_id IN (1,2,3,4,5,6)
ORDER BY report_id ASC, time_sent DESC
结果如下:
report_id time_sent
1 2
1 1
2 4
2 3
3 4
我想更改该查询,以便我将获得一个 DISTINCT report_id 及其 max(time_sent),例如:
report_id time_sent
1 2
2 4
3 4
如何以最有效的方式做到这一点?谢谢。
Let's say I have this query:
SELECT report_id, time_sent
FROM report_submissions
WHERE report_id IN (1,2,3,4,5,6)
ORDER BY report_id ASC, time_sent DESC
with this result:
report_id time_sent
1 2
1 1
2 4
2 3
3 4
And I want to change that query so I will get a DISTINCT report_id with its max(time_sent), for example:
report_id time_sent
1 2
2 4
3 4
How do I do that in the most efficient way? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DISTINCT
是一个懒惰的GROUP BY
!DISTINCT
is a lazyGROUP BY
!您需要使用
group by
请参阅:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
you need to use
group by
see: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html