Select語句-子查詢
子查詢
指嵌入在其他sql語句中的select語句,也叫嵌套查詢
- 單行子查詢:使用正常的關系表達符號 < > = !=
- 多行子查詢:使用關鍵字 in
用在where條件中:用來輔助篩選條件
用在from條件中:用來作為臨時表一起組合返回結果數據,解決復雜問題非常有效
-
使用all操作符:
- select ename,sal, deptno from emp where sal > all(select sal from emp where deptno = 30)
- 大於所有的為真
-
使用any操作符:
- select ename,sal, deptno from emp where sal > any(select sal from emp where deptno = 30)
- 大於任意的為真
-
多列子查詢
查詢返回多個列數據
(字段1,字段2) = (select 字段1,字段2 from ...)
注意字段1和字段2對比時順序要一致