平時工作中,很少用any、some、all,今天突然看到這種用法,感覺很不錯,工作中應該能用到;
用some,any和all對子查詢中返回的多行結果進行處理。
下面我們來簡單介一下這幾個關鍵詞的含義。
* Some在此表示滿足其中一個的意義,是用or串起來的比較從句。
* Any也表示滿足其中一個的意義,也是用or串起來的比較從句,區別是any一般用在非“=”的比較關係中,這也很好理解,英文中的否定句中使用any肯定句中使用some,這一點是一樣的。
* All則表示滿足其其中所有的查詢結果的含義,使用and串起來的比較從句。
Any
帶【any】的嵌套查詢和【some】的嵌套查詢功能是一樣的。早期的SQL僅僅允許使用【any】,後來的版本爲了和英語的【any】相區分,引入了【some】,同時還保留了【any】關鍵詞。
any:
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >any(select sal from scott.emp where job='MANAGER');
帶any的查詢過程等價於兩步的執行過程。
(1)執行“select sal from scott.emp where job='MANAGER'”
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 or sal>2850 or sal>2450;
some
some:
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =some(select sal from scott.emp where job='MANAGER');
帶some的嵌套查詢與any的步驟相同。
(1)子查詢,執行“select sal from scott.emp where job='MANAGER'”,其結果如圖4.22所示。
(2)父查詢執行下列語句。
―――――――――――――――――――――――――――――――――――――
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =2975 or sal=2850 or sal=2450;
all
all 是查詢還可以是子查詢
如:
select name from edit
其中name前省略了all.
name前可以加ALL|DISTINCT
all是所有記錄.
distinct是不重複的。