题解 | #统计每种性别的人数#

统计每种性别的人数

https://www.nowcoder.com/practice/f04189f92f8d4f6fa0f383d413af7cb8

-- 01 SUBSTRING_INDEX(str,delim,count) str要处理的字符串,delim分隔符,count取几个:正数从左到右,复数从右到左
SELECT 
SUBSTRING_INDEX(profile ,',',-1) as gender,
COUNT(1) as number 
from user_submit 
GROUP BY gender;

-- 02 INSTR(str,substr) 返回substr在str中第一次出现的位置,未出现则为0
-- 这里数据特点,取female是否出现判断
SELECT 
case when  INSTR(profile ,'female')>0 then 'female' 
     else 'male' end as gender,
COUNT(1) as number 
from user_submit 
GROUP BY gender;

-- 03  IF(expr1,expr2,expr3) if exper1为true则返回exper2的值,false则返回exper3的值
select IF(profile like '%female','female','male') as gender,
COUNT(1) as number
from user_submit
GROUP BY gender 

全部评论

相关推荐

大佬们,在大厂实习的都是几百一天???
爱睡觉的冰箱哥:实习工资这个不都是公开的吗,a不了,字节400,快手350,有些有房补餐补这样
点赞 评论 收藏
分享
qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
喜欢飞来飞去的雪碧在刷代码:可以试一试字节
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务