2011-10-13 53 views
0

比方說,我有這個疑問: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及其最大值(time_sent),例如:

report_id time_sent 
1   2 
2   4 
3   4 

如何以最有效的方式執行此操作?謝謝。

回答

2
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 
0

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 
+0

爲什麼 「MAX(time_sent)DESC」? – Rifat

+0

因爲具有諷刺意味的是,** I **是懶惰的,複製粘貼原件,然後使其有效。 – Widor

相關問題