oracle 中 in 和exists用法區別

2021-07-08 14:47:28 字數 547 閱讀 1860

select * from sc where cno='c001' and sno in(select sno from sc where cno='c002');

select * from sc where cno='c001' and exists(select sno from sc where cno='c002');

請問這兩句執行結果為什麼不一樣?

2013-09-11 20:41

提問者採納

是這樣的

in 是返回的結果集

比如你只執行這一句

select sno from sc where cno='c002'

返回的是一列sno

但是exists則不同,返回的是布林值

雖然裡邊那個沒法單獨執行

select * from sc a where cno='c001' and exists(select sno from sc b where cno='c002' and a.sno=b.sno) ;

後邊必須要寫上兩者的關聯

ORACLE 中exist和in的區別

博文 oracle中的exists 和not exists 用法 博文 in與exists語句的效率問題 一 exists sql 返回結果集為真 notexists sql 不返回結果集為真 如下 表a id name 1a1 2a2 3a3 表b id aid name 11b1 22b2 32...

oracle常識(一) in和exist的區別

in 與 exist 的語法比較 select from 資料表 t where t.x in 括號內可以是符合t.x欄位型別的值集合,如 1 2 3 但如果t.x是number型別的時候,似乎這樣的寫法會出問題 也可以是通過另外的select語句查詢出來的值集合,如 select y from 資...

sql中in和exist語句的區別?

in和exists in 是把外表和內錶作hash 連線,而exists是對外表作loop迴圈,每次loop迴圈再對內表進行查詢。如果兩個表中乙個較小,乙個是大表,則子查詢表大的用exists,子查詢錶小的用in 例如 表a 小表 表b 大表 1 select from a where cc in ...