如何在MySQL中获得多个最大记录

时间:2015-06-23 02:05:48

标签: mysql max

这是我想要的答案。但如果我在MySQL中使用MAX()函数,它只返回一条记录。如何处理它?<​​/ p>

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

1 个答案:

答案 0 :(得分: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
                       )
                 );