当前位置:网站首页>SQL27 View user details of different age groups
SQL27 View user details of different age groups
2022-07-31 22:10:00 【java factory manager】
SQL27 查看不同年龄段的用户明细
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第3天,点击查看活动详情
1、题目
现在运营想要将用户划分为20岁以下,20-24岁,25岁及以上三个年龄段,分别查看不同年龄段用户的明细情况,请取出相应数据.
(注:若年龄为空请返回其他.)
示例:user_profile
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | 北京大学 | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | 复旦大学 | 4 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | 北京大学 | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | 浙江大学 | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | 山东大学 | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | 山东大学 | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | male | 26 | 复旦大学 | 3.6 | 9 | 6 | 52 |
根据示例,你的查询应返回以下结果:
| device_id | gender | age_cut |
|---|---|---|
| 2138 | male | 20-24岁 |
| 3214 | male | 其他 |
| 6543 | female | 20-24岁 |
| 2315 | female | 20-24岁 |
| 5432 | male | 25岁及以上 |
| 2131 | male | 25岁及以上 |
| 4321 | male | 25岁及以上 |
示例1
输入:
drop table if exists `user_profile`;
drop table if exists `question_practice_detail`;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL
);
CREATE TABLE `question_detail` (
`id` int NOT NULL,
`question_id`int NOT NULL,
`difficult_level` varchar(32) NOT NULL
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(8,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(10,2131,113,'right');
INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
INSERT INTO question_practice_detail VALUES(12,2315,115,'right');
INSERT INTO question_practice_detail VALUES(13,2315,116,'right');
INSERT INTO question_practice_detail VALUES(14,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(15,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(16,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(17,2131,113,'right');
INSERT INTO question_practice_detail VALUES(18,5432,113,'wrong');
INSERT INTO question_practice_detail VALUES(19,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(20,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(21,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(22,2131,113,'right');
INSERT INTO question_practice_detail VALUES(23,5432,113,'wrong');
INSERT INTO question_detail VALUES(1,111,'hard');
INSERT INTO question_detail VALUES(2,112,'medium');
INSERT INTO question_detail VALUES(3,113,'easy');
INSERT INTO question_detail VALUES(4,115,'easy');
INSERT INTO question_detail VALUES(5,116,'medium');
INSERT INTO question_detail VALUES(6,117,'easy');
输出:
2138|male|20-24岁
3214|male|其他
6543|female|20-24岁
2315|female|20-24岁
5432|male|25岁及以上
2131|male|25岁及以上
4321|male|25岁及以上
复制代码2、思路🧠
问题分解:
- 限定条件:无;
- 划分年龄段:Numerical condition judgment,Can use multipleif,But it is more convenient to use
case when [expr] then [result1]...else [default] end
解决方法:
IF条件查询
IF(age is null,'其他', IF(age<20,'20岁以下', IF(age<=24,'20-24岁','25岁及以上'))) 复制代码
CASE函数查询,是一种多分支的函数,可以根据条件列表的值返回多个可能的结果表达式中的一个;可用在任何允许使用表达式的地方,但不能单独作为一个语句执行.分为:简单CASE函数、搜索CASE函数
#简单CASE函数 CASE 测试表达式 WHEN 简单表达式1 THEN 结果表达式1 WHEN 简单表达式2 THEN 结果表达式2 …… WHEN 简单表达式n THEN 结果表达式n [ ELSE 结果表达式n+1 ] END #搜索CASE函数 CASE WHEN 布尔表达式1 THEN 结果表达式1 WHEN 布尔表达式2 THEN 结果表达式2 …… WHEN 布尔表达式n THEN 结果表达式n [ ELSE 结果表达式n+1 ] END 复制代码
3、代码
commit AC
SELECT device_id, gender,
(case when age <20 then '20岁以下'
when age >=20 AND age < 25 then '20-24岁'
when age >=25 then '25岁及以上'
else '其他' end) AS age_cut
FROM user_profile
SELECT device_id,gender,
IF(age is null,'其他',
IF(age<20,'20岁以下',
IF(age<=24,'20-24岁','25岁及以上'))) age_cut
FROM user_profile
复制代码4、总结
该题目的对SQL的语法及基础知识,学会使用IF、CASE等的条件查询也要会使用,像内连接、外连接、左连接、右连接等都要有相关的了解,其次当你编写了大量的SQL之后,就要学会进行SQL的优化,这对于数据查询的时间会有大幅度的降低.
️来自专栏《Mysql每日一题》欢迎订阅️
厂长写博客目的初衷很简单,希望大家在学习的过程中少走弯路,多学一些东西,对自己有帮助的留下你的赞赞或者关注都是对我最大的支持,你的关注和点赞给厂长每天更文的动力.
对文章其中一部分不理解,都可以评论区回复我,我们来一起讨论,共同学习,一起进步!
边栏推荐
- Short-circuit characteristics and protection of SiC MOSFETs
- NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
- Shell 脚本 快速入门到实战 -02
- The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
- 返回一个零长度的数组或者空的集合,不要返回null
- sqlite3 simple operation
- -xms -xmx(information value)
- Arduino框架下STM32全系列开发固件安装指南
- 【AcWing】The 62nd Weekly Match 【2022.07.30】
- #yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
猜你喜欢

NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores

嵌入式开发没有激情了,正常吗?

The article you worked so hard to write may not be your original

财务盈利、偿债能力指标

Socket Review and I/0 Model

VOT2021比赛简介

Realization of character makeup

Shell 脚本 快速入门到实战 -02

Write a database document management tool based on WPF repeating the wheel (1)

ReentrantLock原理(未完待续)
随机推荐
【愚公系列】2022年07月 Go教学课程 025-递归函数
How to debug TestCafe
linux查看redis版本命令(linux查看mysql版本号)
Document management and tools in the development process
返回一个零长度的数组或者空的集合,不要返回null
Flex layout in detail
TestCafeSummary
【AcWing】The 62nd Weekly Match 【2022.07.30】
A few permanent free network transmission, convenient and simple (Intranet through tutorials)
Realize serial port receiving data based on STM32 ring queue
JS basic exercises
Arduino框架下STM32全系列开发固件安装指南
Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios
【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
grep command written test questions
sqlite3简单操作
UserAgent resolution
flowable workflow all business concepts
BOW/DOM (top)
Learn about C# anonymous methods