# select if(age>=25,'25岁及以上','25岁以下') as age_cnt, count(*) as number # from user_profile # group by age_cnt; select (case when age>=25 then '25岁及以上' else '25岁以下' end) as age_cnt, count(*) as number from user_profile group by age_cnt;
参考题解。
主要注意语法的使用。
if(条件,结果1,结果2)
(case when 条件 then 结果1
else 结果2 end)
count(*)语句是在group by执行完分组后再执行。