MySQL 的一列与另一最大列不同

发布于 2024-12-09 19:56:31 字数 483 浏览 0 评论 0原文

假设我有这个查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原野 2024-12-16 19:56:31
SELECT report_id, max(time_sent)
FROM  report_submissions
WHERE report_id IN (1,2,3,4,5,6)
GROUP BY report_id
ORDER BY report_id ASC
SELECT report_id, max(time_sent)
FROM  report_submissions
WHERE report_id IN (1,2,3,4,5,6)
GROUP BY report_id
ORDER BY report_id ASC
坏尐絯℡ 2024-12-16 19:56:31

DISTINCT 是一个懒惰的GROUP BY

SELECT report_id, max(time_sent)
FROM  report_submissions
WHERE report_id IN (1,2,3,4,5,6)
GROUP BY report_id
ORDER BY report_id ASC, max(time_sent) DESC 

DISTINCT is a lazy GROUP BY!

SELECT report_id, max(time_sent)
FROM  report_submissions
WHERE report_id IN (1,2,3,4,5,6)
GROUP BY report_id
ORDER BY report_id ASC, max(time_sent) DESC 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文