多表查詢(子查詢)
子查詢, 也稱巢狀查詢
子查詢的語句可以是insert/update/delete/select中的任何一個
根據子查詢的結果不同,可以分為:
-
標量子查詢(結果為單個值)
-
列子查詢(結果為一列)
-
行子查詢(子查詢結果為一行)
-
表子查詢(子查詢結果為多行多列)
select * from t1 where column1 = (select column1 from t2)
標量子查詢
查詢研發部所有員工的資訊
select * from emp
where dept_id in
(select id from dept where name='研發部');
查詢在韋一笑之後(2002-02-05)入職的員工
select * from emp
where entryDate >
(select entryDate from emp where name='韋一笑');
列子查詢
in, not in, any, some, all
查詢總經辦和市場部的員工
select * from emp
where dept_id in
(select id from dept where name in ('總經辦','市場部'));
查詢比市場部所有員工(最大值9000)工資都高的員工
#可以這樣寫,>max
select * from emp
where salary >
(select max(salary) from emp where dept_id = (select id from dept where name='市場部'));
#也可以這樣寫, 用all修飾詞
select * from emp
where salary > all (select salary from emp where dept_id = (select id from dept where name='市場部'));
查詢比研發部任意一人(最小值6600)工資高的員工
select * from emp
where salary > any (select salary from emp where dept_id = (select id from dept where name='研發部'));