MySQL在一个列上与另一个max列不同
假设我有这样一个疑问:MySQL在一个列上与另一个max列不同,mysql,distinct,Mysql,Distinct,假设我有这样一个疑问: 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 我想更改该查询,以便获得一个
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
我想更改该查询,以便获得一个不同的报告id及其最大值(发送时间),例如:
report_id time_sent
1 2
2 4
3 4
我如何以最有效的方式做到这一点?谢谢。
DISTINCT
是一个懒惰的群组成员
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, max(time_sent) DESC
您需要使用
分组依据
请参阅:,具有讽刺意味的是,我很懒,复制粘贴了原件,然后使其有效。