SQL题解 | #查看不同年龄段的用户明细#
查看不同年龄段的用户明细
https://www.nowcoder.com/practice/ae44b2b78525417b8b2fc2075b557592
在理解了上一题的IF后,这一题做起来就是手到擒来啦!
①类似于高级编程语言的which判断逻辑(MySQL中是 case when...then...else... end )
case when condition1 then res1 when condition2 then res2 else res3 end as 重命名字段
②一个易错点:其他
别打错成 `其它` 了,我这个错误找了好久。。。
=====================================
答案:
SELECT device_id, gender, case when age<20 then '20岁以下' when age>=20 and age<25 then '20-24岁' when age>=25 then '25岁及以上' else '其他' end as age_cut FROM user_profile