思路
使用 if 来对年龄进行分组
实现
1
这个是参考题解区大佬改进的,代码更少。
select if(age < 25 or age is NULL,"25岁以下", "25岁及以上" ) as age_cut,count(device_id) as number from user_profile
group by age_cut
2
这个是我自己想出来的,但是代码更多
select age_cut, COUNT(age_cut) as number from
(select if(age < 25 or age is NULL,"25岁以下", "25岁及以上" ) as age_cut from user_profile) as age_cut
group by age_cut