SQL - 返回具有最高计数的行

时间:2017-12-12 17:17:58

标签: mysql sql

我正在尝试返回计数最高的行,但这不起作用。如果有人可以帮助我,我将不胜感激。谢谢!

(select school_name, count (school_name) as 'school'
  from ad_last_org_info_v
  where ad_last_org_info.id_num = ad_org_tracking.id_num
  group by school_name
  order by 'school' desc limit 1))

例如,如果学校30计数5次,学校20计数3次,数据将显示学校30.它取最高计数并返回1结果。

3 个答案:

答案 0 :(得分:0)

select school_name,Count from (
select school_name,count(1) as Count from ad_last_org_info_v
group by school_name
) A order by Count desc LIMIT 1

答案 1 :(得分:0)

试试这个;

select * from (
select school_name, count (school_name) as 'school' from 
ad_last_org_info_v where ad_last_org_info.id_num = 
ad_org_tracking.id_num group by school_name order by 'school' desc) Records
limit 1

答案 2 :(得分:0)

我会用HAVING找到所有最高学校名称

select school_name, count (school_name) as 'school'
from ad_last_org_info_v
group by school_name
HAVING COUNT(school_name) >= all(
    select count (school_name)
    from ad_last_org_info_v
    group by school_name
 )