题解 | 截取出年龄
截取出年龄
https://www.nowcoder.com/practice/b8d8a87fe1fc415c96f355dc62bdd12f
select substring_index(substring_index(profile,',',3),',',-1) as age, count(device_id) as number from user_submit group by age
问题分解:
- 限定条件:无;
- 每个年龄:按年龄分组group by age,但是没有age字段,需要从profile字段截取,按字符,分割后取出即可。可使用substring_index函数可以按特定字符串截取源字符串。substring_index(FIELD, sep, n)可以将字段FIELD按照sep分隔:(1).当n大于0时取第n个分隔符(n从1开始)之后的全部内容;(2).当n小于0时取倒数第n个分隔符(n从-1开始)之前的全部内容;因此,本题可以先用substring_index(profile, ',', 3)取出"180cm,75kg,27",然后用substring_index(profile, ',', -1)取出27。当然也可以用substring_index(substring_index(profile, ",", -2), ",", 1)取出27。附:substring_index函数解析
- 多少参赛者:计数统计,count(device_id)
细节问题:
- 表头重命名:as