SQL30. 统计每种性别的人数
描述
device_id | profile | blog_url |
2138 | 180cm,75kg,27,male | http:/url/bigboy777 |
3214 | 165cm,45kg,26,female | http:/url/kittycc |
6543 | 178cm,65kg,25,male | http:/url/tiger |
4321 | 171cm,55kg,23,female | http:/url/uhksd |
2131 | 168cm,45kg,22,female | http:/urlsydney |
gender | number |
male | 2 |
female | 3 |
示例1
输入:
drop table if exists user_submit; CREATE TABLE `user_submit` ( `id` int NOT NULL, `device_id` int NOT NULL, `profile` varchar(100) NOT NULL, `blog_url` varchar(100) NOT NULL ); INSERT INTO user_submit VALUES(1,2138,'180cm,75kg,27,male','http:/url/bisdgboy777'); INSERT INTO user_submit VALUES(1,3214,'165cm,45kg,26,female','http:/url/dkittycc'); INSERT INTO user_submit VALUES(1,6543,'178cm,65kg,25,male','http:/url/tigaer'); INSERT INTO user_submit VALUES(1,4321,'171cm,55kg,23,female','http:/url/uhsksd'); INSERT INTO user_submit VALUES(1,2131,'168cm,45kg,22,female','http:/url/sysdney');
输出:
male|2 female|3
Mysql 解法, 执行用时: 36ms, 内存消耗: 6380KB, 提交时间: 2021-12-02
SELECT substring_index(profile, ',', -1) as gender,COUNT(*) AS number FROM user_submit group by gender;
Mysql 解法, 执行用时: 36ms, 内存消耗: 6388KB, 提交时间: 2022-02-09
select substring_index(profile,',',-1) as gender,count(device_id) from user_submit group by gender
Mysql 解法, 执行用时: 36ms, 内存消耗: 6388KB, 提交时间: 2021-12-18
SELECT IF(u.profile LIKE '%female','female','male'),COUNT(1) FROM user_submit u GROUP BY IF(u.profile LIKE '%female','female','male');
Mysql 解法, 执行用时: 36ms, 内存消耗: 6396KB, 提交时间: 2021-12-03
select SUBSTRING_INDEX(profile,",",-1) as gender,count(device_id) from user_submit group by gender
Mysql 解法, 执行用时: 36ms, 内存消耗: 6400KB, 提交时间: 2022-01-25
select SUBSTRING_INDEX(profile,',',-1) as gender, count(device_id)as number FROM user_submit group by gender