2015-06-23 64 views
2

這是我想要得到的答案。但是如果我在MySQL中使用MAX()函數,它只會返回一條記錄。如何處理它?如何在MySQL中獲得多個最大記錄

A_plus ID 
2  12345 
2  45678 

如上描述下,我作爲同行的SQL,但它只是返回一條記錄。

SELECT MAX(A_plus_Num) AS A_plus, ID FROM 
(SELECT COUNT(grade) AS A_plus_Num,ID FROM take WHERE grade = 'A+'GROUP BY ID) AS temp 


A_plus ID 
2  12345 

回答

2

在MySQL中,查詢有點複雜。一種方法是使用兩個子集進行聚合:

select t.* 
from (select t.id, count(*) as A_plus 
     from take t 
     where t.grade = 'A+' 
     group by t.id 
    ) t 
where t.A_plus = (select max(A_plus) 
        from (select t.id, count(*) as a_plus 
         from take t 
         where t.grade = 'A+' 
         group by t.id 
         ) 
       ); 
相關問題