一般取不同id对应出现次数是这样:
select id,count(1) as ct from table_name group by id
但如果要聚合的内容很长:
select if(class = 'high' and school in ('1', '2', '3'), 1, 0) as class_type,count(1) as ct
from table_name
group by if(class = 'high' and school in ('1', '2', '3'), 1, 0)
此时,简化group by写法
select if(class = 'high' and school in ('1', '2', '3'), 1, 0) as class_type,count(1) as ct
from table_name
group by 1
两者含义相同
因此:group by 后面1,2,3数字表示按照select后面几个属性进行聚合,1,2,3就是selct后面的3个属性,1就是select后面1个属性
order by同理
参考:group by和order by用法,group by 1 和 order by 1含义_二楼后座Tansen的博客-CSDN博客