MySQL子查询及其案例

MySQL子查询及其案例

子查询

在查询中嵌套另一个查询

子查询案例

sql 复制代码
-- 创建部门表
create table department(
	dept_id int comment '部门id',
	dept_name varchar(10) comment '部门名称'
)comment '部门表';

-- 给部门表添加数据
insert into department values
	(1, '总部'),
	(2, '开发部'),
	(3, '咨询部'),
	(4, '财务部'),
	(5, '行政部'),
	(6, '人力部');

-- 创建经理表
create table manager(
	manager_id int comment '经理id',
	manager_name varchar(10) comment '经理名字',
	dept_id int comment '部门id'
)comment '经理表';

-- 给经理表添加数据
insert into manager values
	(1, '黄文隆', 1),
	(2, '张吉', 2),
	(3, '林玟', 3),
	(4, '林雅', 3),
	(5, '江奕', 3),
	(6, '刘柏', 4),
	(7, '吉茹', 4);
	
-- 创建员工表
create table employee(
	emp_id int,
	emp_name varchar(10),
	job_position enum(
		'CEO',
		'开发人员',
		'顾问',
		'经理',
		'HR',
		'会计',
		'行政'
	),
	salary decimal(10, 2),
	dept_id int,
	manager_id int 
);

-- 给员工表添加数据
insert into employee(emp_id, emp_name, job_position, salary, dept_id, manager_id) 
values 
(101, '黄文隆', 'CEO', 100000.00, 1, NULL),
(102, '张吉',   '经理', 30000.00, 2, 1),
(103, '谢彦',   '开发人员', 15000.00, 2, 2),
(104, '傅智翔', '开发人员', 12000.00, 2, 2),
(105, '林玟',   '经理', 35000.00, 3, 1),
(106, '荣姿康', '顾问', 12000.00, 3, 3),
(107, '林雅',   '经理', 32500.00, 3, 1),
(108, '雷进宝', '顾问', 12500.00, 3, 4),
(109, '江奕',   '经理', 31000.00, 3, 1),
(110, '李雅惠', '顾问', 26000.00, 3, 5),
(111, '吴佳瑞', 'HR',  12000.00, NULL, NULL),
(112, '周琼玟', '会计', 14000.00, NULL, NULL),
(113, '黄文',   '行政', 19000.00, NULL, NULL);

select * from department;   # 结果如下图1
select * from manager;      # 结果如下图2
select * from employee;     # 结果如下图3


图1. 部门表

图2. 经理表

图3. 员工表

sql 复制代码
-- 查询由咨询部的经理管理的员工
-- 1.查询咨询部对应的部门id
select dept_id from department where dept_name='咨询部';  # 结果为 3
-- 2.查询第一步得到的部门id对应的经理id
select manager_id from manager where dept_id=3;  # 结果为 3 4 5
-- 3.查询第二步得到的经理id对应的员工
select emp_name, job_position, salary from employee where manager_id in(3, 4, 5);  # 结果如下图4

-- 改为子查询的写法
select emp_name, job_position, salary 
from employee 
where manager_id in(
	select manager_id 
	from manager 
	where dept_id=(
		select dept_id 
		from department 
		where dept_name='咨询部'
	)
);   # 结果如下图4


图4. 由咨询部的经理管理的员工

sql 复制代码
-- 查询职位为开发人员或顾问的员工所属的部门名称
-- 1.查询开发人员或顾问对应的部门id
select dept_id from employee where job_position in('开发人员', '顾问');  # 结果为 2 2 3 3 3
-- 2.查询由第一步得到的部门id对应的部门名称
select dept_name from department where dept_id in(2, 3);  # 结果为 开发部 咨询部

-- 改为子查询的写法
select  distinct dept_name 
from department 
where dept_id in(
	select dept_id 
	from employee 
	where job_position 
	in('开发人员', '顾问')
);   # 结果为 开发部 咨询部
sql 复制代码
-- 查询薪资高于任意一个开发人员的非开发人员 
-- 任意 any()
select emp_name as 员工姓名, job_position as 职位, salary as 薪资
from employee 
where job_position != '开发人员'
and salary > any(
	select salary 
	from employee 
	where job_position='开发人员'
);   # 结果如下图5


图5. 薪资高于任意一个开发人员的非开发人员

相关推荐
m0_736919104 分钟前
用Pandas处理时间序列数据(Time Series)
jvm·数据库·python
亓才孓4 分钟前
[JDBC]PreparedStatement替代Statement
java·数据库
m0_4665252931 分钟前
绿盟科技风云卫AI安全能力平台成果重磅发布
大数据·数据库·人工智能·安全
爱学习的阿磊1 小时前
使用Fabric自动化你的部署流程
jvm·数据库·python
枷锁—sha1 小时前
【SRC】SQL注入快速判定与应对策略(一)
网络·数据库·sql·安全·网络安全·系统安全
惜分飞1 小时前
ORA-600 kcratr_nab_less_than_odr和ORA-600 4193故障处理--惜分飞
数据库·oracle
chian-ocean1 小时前
CANN 生态进阶:利用 `profiling-tools` 优化模型性能
数据库·mysql
m0_550024631 小时前
持续集成/持续部署(CI/CD) for Python
jvm·数据库·python
AC赳赳老秦2 小时前
代码生成超越 GPT-4:DeepSeek-V4 编程任务实战与 2026 开发者效率提升指南
数据库·数据仓库·人工智能·科技·rabbitmq·memcache·deepseek
啦啦啦_99992 小时前
Redis-2-queryFormat()方法
数据库·redis·缓存