SQL8 查找某个年龄段的用户信息

描述

题目:现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。

用户信息表:user_profile

|----|-----------|--------|-----|------------|----------|
| id | device_id | gender | age | university | province |
| 1 | 2138 | male | 21 | 北京大学 | Beijing |
| 2 | 3214 | male | | 复旦大学 | Shanghai |
| 3 | 6543 | female | 20 | 北京大学 | Beijing |
| 4 | 2315 | female | 23 | 浙江大学 | ZheJiang |
| 5 | 5432 | male | 25 | 山东大学 | Shandong |

根据输入,你的查询应返回以下结果:

|-----------|--------|-----|
| device_id | gender | age |
| 2138 | male | 21 |
| 6543 | female | 20 |
| 2315 | female | 23 |

示例1

输入:

复制代码
drop table if exists user_profile;
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,
`province` varchar(32)  NOT NULL);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

复制输出:

复制代码
2138|male|21
6543|female|20
2315|female|23

题解

between and 可以用于确定范围

|---|-----------------------------------------------------------------------------------|
| 1 | SELECT device_id,gender,age FROM user_profile WHERE age BETWEEN ``20 AND ``23 |

根据输入,你的查询应返回以下结果:

相关推荐
黑听人11 分钟前
【力扣 简单 C】141. 环形链表
c语言·开发语言·数据结构·算法·leetcode
Blossom.1181 小时前
基于深度学习的智能图像分类系统:从零开始构建
开发语言·人工智能·python·深度学习·神经网络·机器学习·分类
缘友一世1 小时前
java设计模式[2]之创建型模式
java·开发语言·设计模式
Edingbrugh.南空1 小时前
Hive SQL:一小时快速入门指南
hive·hadoop·sql
vace cc2 小时前
sql列中数据通过逗号分割的集合,对其中的值进行全表查重
数据库·sql
cyc&阿灿2 小时前
Java中extends与implements深度解析:继承与接口实现的本质区别
java·开发语言
liujing102329294 小时前
Day13_C语言基础&项目实战
c语言·开发语言
周振超的4 小时前
c++编译第三方项目报错# pragma warning( disable: 4273)
开发语言·c++
JH30735 小时前
Java Stream API 在企业开发中的实战心得:高效、优雅的数据处理
java·开发语言·oracle
呆呆的小草8 小时前
Cesium距离测量、角度测量、面积测量
开发语言·前端·javascript