题解 | #计算25岁以上和以下的用户数量#
计算25岁以上和以下的用户数量
https://www.nowcoder.com/practice/30f9f470390a4a8a8dd3b8e1f8c7a9fa
select case when age <25 or age is null then '25岁以下' when age >= 25 then '25岁及以上' end as age_cut,count(*) as number from user_profile group by age_cut # 1:只要有聚合函数 sum(),count(),max(),avg() 等函数就需要用到 group by , 否则就会报上面的错误. # 2:group by id (id 是主键) 的时候, select 什么都没有问题, 包括有聚合函数.类似select count(*) from 其实就是一个以主键为组的分组 # 3:group by role (非主键) 的时候, select 只能是聚合函数和 role ( group by 的字段) , 否则报错