当前位置:网站首页>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每日一题》欢迎订阅️
厂长写博客目的初衷很简单,希望大家在学习的过程中少走弯路,多学一些东西,对自己有帮助的留下你的赞赞或者关注都是对我最大的支持,你的关注和点赞给厂长每天更文的动力.
对文章其中一部分不理解,都可以评论区回复我,我们来一起讨论,共同学习,一起进步!
边栏推荐
- BM5 合并k个已排序的链表
- 顺序表的实现
- C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
- AI 自动写代码插件 Copilot(副驾驶员)
- [Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
- Verilog implements a divide-by-9 with a duty cycle of 5/18
- sqlite3 simple operation
- A solution to the server encountered an internal error that prevented it from fulfilling this request [easy to understand]
- UVM RAL model and built-in seq
- ThreadLocal
猜你喜欢
随机推荐
STM32 full series development firmware installation guide under Arduino framework
【AcWing】The 62nd Weekly Match 【2022.07.30】
Basic Grammar Introduction of Carbon Tutorial (Tutorial)
How to get useragent
Quick Start Tutorial for flyway
返回一个零长度的数组或者空的集合,不要返回null
UVM RAL model and built-in seq
C程序设计-方法与实践(清华大学出版社)习题解析
The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
第六章
iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools
Arduino框架下STM32全系列开发固件安装指南
Realization of character makeup
[NLP] What is the memory of the model!
Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization
Short-circuit characteristics and protection of SiC MOSFETs
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
Chapter Six
#yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
全网一触即发,自媒体人的内容分发全能助手——融媒宝