一.子查询
二.标量子查询
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 = '咨询部');