当前位置:网站首页>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每日一题》欢迎订阅️
厂长写博客目的初衷很简单,希望大家在学习的过程中少走弯路,多学一些东西,对自己有帮助的留下你的赞赞或者关注都是对我最大的支持,你的关注和点赞给厂长每天更文的动力.
对文章其中一部分不理解,都可以评论区回复我,我们来一起讨论,共同学习,一起进步!
边栏推荐
- 了解下C# 匿名方法
- Returns a zero-length array or empty collection, do not return null
- 支付模块实现
- Structure of the actual combat battalion module eight operations
- OSPFv3的基本配置
- Daily practice——Randomly generate an integer between 1-100 and see how many times you can guess.Requirements: The number of guesses cannot exceed 7 times, and after each guess, it will prompt "bigger"
- Arduino框架下STM32全系列开发固件安装指南
- Financial profitability and solvency indicators
- 老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
- GateWay implements load balancing
猜你喜欢

21. Support Vector Machine - Introduction to Kernel Functions

NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
Dry goods | 10 tips for MySQL add, delete, change query performance optimization
![[NLP] What is the memory of the model!](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[NLP] What is the memory of the model!

The old music player WinAmp released version 5.9 RC1: migrated to VS 2019, completely rebuilt, compatible with Win11

MATLAB program design and application 2.4 Common internal functions of MATLAB

Getting Started with Tkinter
![[Intensive reading of the paper] iNeRF](/img/a7/910667911e1ce8996b9d22de63ea04.png)
[Intensive reading of the paper] iNeRF

C程序设计-方法与实践(清华大学出版社)习题解析

OSPFv3的基本配置
随机推荐
支付模块实现
高效并发:Synchornized的锁优化详解
NVIDIA已经开始测试AD106和AD107 GPU核心的显卡产品
架构实战营模块八作业
What is Thymeleaf?How to use.
Basic configuration of OSPFv3
Pytorch lstm time series prediction problem stepping on the pit "recommended collection"
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
"The core concept of" image classification and target detection in the positive and negative samples and understanding architecture
20. Support vector machine - knowledge of mathematical principles
【愚公系列】2022年07月 Go教学课程 025-递归函数
In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved
ECCV 2022 Huake & ETH propose OSFormer, the first one-stage Transformer framework for camouflaging instance segmentation!The code is open source!...
Flex layout in detail
C程序设计-方法与实践(清华大学出版社)习题解析
Learn about C# anonymous methods
find prime numbers up to n
Go1.18 upgrade function - Fuzz test from scratch in Go language
Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...
Judging decimal points and rounding of decimal operations in Golang