MySQL-25.多表查询-子查询(标量、列)

一.子查询

二.标量子查询

sql 复制代码
-- ============================================= 子查询 ==============================================
-- 标量子查询
-- A. 查询"教研部"的所有员工信息
-- a. 查询教研部的部门id    tb_dept
select id from tb_dept where name = '教研部';

-- b. 查询部门id为2的所有员工信息   tb_emp
select * from tb_emp where dept_id = 2;

-- 将两条子查询合并
select * from tb_emp where dept_id = (select id from tb_dept where name = '教研部');

-- B. 查询在"方东白"入职之后的员工信息
-- a.查询"方东白"的入职时间   tb-emp
select entrydate from tb_emp where name = '方东白';

-- b.查询entrydate大于方东百的员工信息  tb-emp
select * from tb_emp where entrydate > '2012-11-01';

select * from tb_emp where entrydate > (select entrydate from tb_emp where name = '方东白');

三.列子查询

sql 复制代码
-- 列子查询
-- A.查询"教研部"和"咨询部"的所有员工信息
-- a.查询"教研部"和"咨询部"的部门id
select id from tb_dept where name = '教研部' or name = '咨询部';

-- b.查询员工部门id等于2或者3的员工信息
select * from tb_emp where dept_id in (3,2);

select * from tb_emp where dept_id in (select id from tb_dept where name = '教研部' or name = '咨询部');
相关推荐
Apple_羊先森1 小时前
ORACLE数据库巡检SQL脚本--22、检查碎片程度最高的业务表
数据库·sql·oracle
OnYoung2 小时前
更优雅的测试:Pytest框架入门
jvm·数据库·python
山岚的运维笔记3 小时前
SQL Server笔记 -- 第85章:查询提示
数据库·笔记·sql·microsoft·sqlserver
chilavert3183 小时前
技术演进中的开发沉思-371:final 关键字(中)
java·前端·数据库
tryCbest3 小时前
SQL Server数据库
数据库·sql server
_codemonster4 小时前
PreparedStatement 和 Statement的区别
数据库·oracle
恒云客4 小时前
python uv debug launch.json
数据库·python·json
愈努力俞幸运5 小时前
第5章数据库,实体关系图,ER图
数据库·oracle