我的表結構是(ID,集羣,QID,優先級)。我試圖弄清楚如何顯示每個羣集的最大優先級值。說羣集1有優先級100,102,105。我想顯示包含105的記錄。請幫助。MySQL查詢問題上組和最大
回答
這裏是一篇文章,解釋如何選擇每個組的最大值的行。
http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
謝謝!這工作! – 1s2a3n4j5e6e7v 2010-06-14 08:41:27
+1這是文章也幫助我... – 2011-02-18 10:03:48
select cluster, MAX(priority) from structure group by cluster;
要查找所有的列TRY
select * from structure
where priority = (
select MAX(priority) from structure as s
where s.cluster = structure.cluster
);
不,這是行不通的。它不返回集羣最高優先級的記錄。只顯示最大值。 – 1s2a3n4j5e6e7v 2010-06-14 08:14:30
好吧,然後嘗試我編輯的答案。 – Salil 2010-06-14 08:21:33
您可以用內篩選出的行加入,如:
select s.*
from structure s
join (
select cluster, MAX(priority) maxprio
from structure
group by
cluster
) filter
on s.cluster = filter.cluster
and s.priority = filter.maxprio
這將返回多行如果它們都具有該羣集的最高優先級。
這個工程!感謝您的直接幫助。與邏輯http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/相比,這似乎有點慢。如果您願意,請嘗試檢查。 – 1s2a3n4j5e6e7v 2010-06-14 08:41:05
- 1. 來自子查詢和問題組的MySQL最大值
- 2. mysql大查詢問題
- 3. MySQL查詢分組問題
- 4. MySQL查詢 - 分組問題
- 5. MySQL查詢分組子查詢問題
- 6. PHP和MySQL查詢問題
- 7. MYSQL查詢問題和子查詢
- 8. MySQL查詢到最大event_count組?
- 9. MySQL查詢的最小值和最大值分組
- 10. Java數組最小和最大問題
- 11. MySQL查詢問題
- 12. MySQL查詢問題
- 13. MySQL查詢問題
- 14. MySQL查詢問題
- 15. MySQL查詢問題
- 16. MySQL查詢問題
- 17. mysql查詢問題
- 18. MySQL查詢問題
- 19. MySQl查詢問題
- 20. MySQL查詢問題
- 21. Mysql查詢問題
- 22. MySQL查詢問題
- 23. MySQL查詢問題
- 24. MySQL查詢問題
- 25. MYSQL查詢問題
- 26. MySQL查詢問題
- 27. MySQL查詢問題
- 28. MySQL查詢問題
- 29. MySQL查詢問題
- 30. MySQL查詢問題
請發表您已有的 – 2010-06-14 08:10:56