当前位置:网站首页>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每日一题》欢迎订阅️
厂长写博客目的初衷很简单,希望大家在学习的过程中少走弯路,多学一些东西,对自己有帮助的留下你的赞赞或者关注都是对我最大的支持,你的关注和点赞给厂长每天更文的动力.
对文章其中一部分不理解,都可以评论区回复我,我们来一起讨论,共同学习,一起进步!
边栏推荐
- 每月一书(202207):《Swift编程权威指南》
- Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
- Flex layout in detail
- Linux环境redis集群搭建「建议收藏」
- -xms -xmx(information value)
- 全网一触即发,自媒体人的内容分发全能助手——融媒宝
- Recognize anomalies (you will understand after reading this)
- LevelSequence source code analysis
- uniapp小程序检查、提示更新
- [Intensive reading of the paper] iNeRF
猜你喜欢

iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools

【论文精读】iNeRF

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

Embedded development has no passion, is it normal?

OSPFv3的基本配置

In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved

NVIDIA已经开始测试AD106和AD107 GPU核心的显卡产品

Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...

Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization

嵌入式开发没有激情了,正常吗?
随机推荐
c语言解析json字符串(json对象转化为字符串)
财务盈利、偿债能力指标
Commonly used security penetration testing tools (penetration testing tools)
ECCV 2022 Huake & ETH propose OSFormer, the first one-stage Transformer framework for camouflaging instance segmentation!The code is open source!...
Bionic caterpillar robot source code
有一说一,外包公司到底值不值得去?
Collation of knowledge points in Ningbo University NBU IT project management final exam
Several methods for deleting specified elements in Golang slices
Implementation of a sequence table
Transfer Learning - Domain Adaptation
[Code Hoof Set Novice Village 600 Questions] Merge two numbers without passing a character array
#yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
Socket Review and I/0 Model
Mobile web development 02
Audio alignment using cross-correlation
UVM RAL model and built-in seq
ReentrantLock原理(未完待续)
请问我的这段sql中sql语法哪里出了错
Niuke.com brush questions (1)
Thymeleaf是什么?该如何使用。