SQL32. 截取出年龄
描述
device_id | profile | blog_url |
2138 | 180cm,75kg,27,male | http:/ur/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:/url/sydney |
age | number |
27 | 1 |
26 | 1 |
25 | 1 |
23 | 1 |
22 | 1 |
示例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');
输出:
27|1 26|1 25|1 23|1 22|1
Mysql 解法, 执行用时: 36ms, 内存消耗: 6388KB, 提交时间: 2022-02-10
SELECT age,COUNT(*) number FROM (SELECT SUBSTR(PROFILE,12,2) age FROM user_submit) ages GROUP BY age
Mysql 解法, 执行用时: 36ms, 内存消耗: 6404KB, 提交时间: 2021-12-14
SELECT substring_index(substring_index(profile,',',-2),',',1) as age,count(*) as number from user_submit group by age;
Mysql 解法, 执行用时: 36ms, 内存消耗: 6416KB, 提交时间: 2022-02-09
SELECT substring_index(SUBSTRING_INDEX(profile,',',-2),',',1) age, count(*) FROM user_submit GROUP BY age
Mysql 解法, 执行用时: 36ms, 内存消耗: 6420KB, 提交时间: 2021-12-27
select SUBSTR(profile,12,2) as age, count(device_id) as number from user_submit group by age
Mysql 解法, 执行用时: 36ms, 内存消耗: 6420KB, 提交时间: 2021-12-20
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(profile,',',3),',',-1) age ,count(device_id) number from user_submit group by age