题解 | 截取出年龄

截取出年龄

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
全部评论

相关推荐

求面试求offer啊啊啊啊:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务