Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL在一个列上与另一个max列不同_Mysql_Distinct - Fatal编程技术网

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 

您需要使用
分组依据

请参阅:

,具有讽刺意味的是,我很懒,复制粘贴了原件,然后使其有效。