在oracle 中,查詢語句用 in 和 or 查詢的結果條數不一樣
先試試不要and a_devtestdt is null 這句、查看兩段代碼結果。
再試試下面
select dept from A
where (status='[D2]方案設計階段' or status='[D2]方案審核中' or status ='[D2]開發中' or status='[D2]開發完成' or status='[D2]單元測試進行中')
and a_devtestdt is null
-----------------------
select dept from A
where status in('[D2]方案設計階段','[D2]方案審核中','[D2]開發中','[D2]開發完成','[D2]單元測試進行中')
and a_devtestdt is null
一般有 OR 和 AND 同時出現的where 條件、最好加括號區分
Oracle語句中IN和=的區別有哪些
in 表示在一個結合內進行查詢,比如 select * from character where letter in ('A','B','C')。
=的作用就是一個值的比較。
但是等號也可以實現in的效果,只是寫起來比較麻煩。比如上面的例子,你也可以這樣寫
select * from character where letter='A' or letter='B' or letter='C'.
兩個運算符都比較常用,根據具體的情況選擇吧
oracle 中 in 不能超過1000的解決方法 具體的解決方法
簡單的可以這樣解決 a IN (1,2,。,999) or a in (1000,1001,。1999) or 。.
或者將in里的存放到一個臨時表里,再關聯查詢!如:
with t as (
select 1 id from dual
union all
select 199999 id from dual)
select 。 from 表 where a in (select * from t)
請問oracle中=與in有什么區別,求大神
首先應用范圍不一樣:
in 可以理解為是范圍內的選擇
= 只有一個
例如:
select sno, sname from t1 where sno in ('sn1001','sn1002');
select sno, sname from t1 where sno in ('sn1001');
select sno, sname from t1 where sno ='sn1001';
select sno, sname from t1 where sno in (select sno from t2); --子查詢結果可以不止一個結果
select sno, sname from t1 where sno =(select sno from t2); --子查詢結果只能有一個
其次性能也不一樣;=的性能大于in的性能,因為=能較好的使用索引等
oracle exists和in的區別
1、關于在 Oracle8i 時代中in和exists的區別 這里有條SQL語句:select * from A where id in(select id from B) 以上查詢使用了in語句,in()只執行一次,它查出B表中的所有id字段并緩存起來.之后,檢查A表的id是否與B表中的id相等,如果相等則將A表的記錄加入結果集中,直到遍歷完A表的所有記錄; 它的查詢過程類似于以下過程 List resultSet=[]; Array A=(select * from A); Array B=(select id from B); for(int i=0;iin(),因為它會B表數據全部遍歷一次.如:A表有10000條記錄,B表有1000000條記錄,那么最多有可能遍歷10000*1000000次,效率很差.再如:A表有10000條記錄,B表有100條記錄,那么最多有可能遍歷10000*100次,遍歷次數大大減少,效率大大提升.結論1:in()適合B表比A表數據小的情況 這里還有一條SQL語句: select a.* from A a where exists(select 1 from B b where *=*) 以上查詢使用了exists語句,exists()會執行*次,它并不緩存exists()結果集,因為exists()結果集的內容并不重要,重要的是結果集中是否有記錄,如果有則返回true,沒有則返回false.它的查詢過程類似于以下過程 List resultSet=[]; Array A=(select * from A) for(int i=0;i是否有記錄返回 *(A[i]); } } return resultSet; 結論2:exists()適合B表比A表數據大的情況 當B表比A表數據大時適合使用exists(),因為它沒有那么遍歷操作,只需要再執行一次查詢就行.如:A表有10000條記錄,B表有1000000條記錄,那么exists()會執行10000次去判斷A表中的id是否與B表中的id相等.如:A表有10000條記錄,B表有100000000條記錄,那么exists()還是執行10000次,因為它只執行*次,可見B表數據越多,越適合exists()發揮效果.再如:A表有10000條記錄,B表有100條記錄,那么exists()還是執行10000次,還不如使用in()遍歷10000*100次,因為in()是在內存里遍歷比較,而exists()需要查詢數據庫,我們都知道查詢數據庫所消耗的性能更高,而內存比較很快.當A表數據與B表數據一樣大時,in與exists效率差不多,可任選一個使用.In適合內外表都很大的情況,exists適合外表結果集很小的情況。
In和exists對比:若子查詢結果集比較小,優先使用in,若外層查詢比子查詢小,優先使 用exists。因為若用in,則Oracle會優先查詢子查詢,然后匹配外層查詢,若使用exists,則oracle會優先查詢外層表,然后再與內層表匹配。
最優化 匹配原則,拿最小記錄匹配大記錄2、關于在 Oracle8i 之后 時代中in和exists的區別 in 是把外表和內表作hash join,而exists是對外表作loop,每次loop再對內表進行查詢。一直以來認為exists比in效率高的說法是不準確的。
如果查詢的兩個表大小相當,那么用in和exists差別不大。如果兩個表中一個較小,一個是大表,則子查詢表大的用exists,子查詢表小的用in:例如:表A(小表),表B(大表)1:select * from A where cc in (select cc from B) 效率低,用到了A表上cc列的索引;select * from A where exists(select cc from B where cc=*) 效率高,用到了B表上cc列的索引。
相反的2:select * from B where cc in (select cc from A) 效率高,用到了B表上cc列的索引;select * from B where exists(select cc from A where cc=*) 效率低,用到了A表上cc列的索引。帶in的關聯子查詢是多余的,因為in子句和子查詢中相關的操作的功能是一樣的。
如:select staff_name from staff_member where staff_id in(select staff_id from staff_func where staff_*_id=staff_*_id); 為非關聯子查詢指定exists子句是不適當的,因為這樣會產生笛卡乘積。如: select staff_name from staff_member where staff_id exists (select staff_id from staff_func); not in 和not exists 如果查詢語句使用了not in 那么內外表都進行全表掃描,沒有用到索引;而not extsts 的子查詢依然能用到表上的索引。
所以無論哪個表大,用not exists都比not in要快。盡量不要使用not in子句。
使用minus 子句都比not in 子句快,雖然使用minus子句要進行兩次查詢: select staff_name from staff_member where staff_id in (select staff_id from staff_member minus select staff_id from staff_func where func_id like '81%'); in 與 "=" 的區別 select name from student where name in ('zhang','wang','li','zhao'); 與 select name from student where name='zhang' or name='li' or name='wang' or name='zhao' 的結果是相同的。3、關于在 Oracle8i 之后 時代中in和exists的區別 在ORACLE 11G大行其道的今天,還有很多人受早期版本的影響,記住一些既定的規則,1.子查詢結果集小,用IN2.外表小,子查詢表大,用EXISTS 這是完全錯誤的觀點。
在8i時代,這經常是正確的,但是現在已經11G了,馬上12C就要面世了。其實在ORACLE 9i CBO就已經優化了IN,EXISTS的區別,ORACLE優化器。
oracle中in和exist的區別
in 和 exists區別
in 是把外表和內表作hash join,而exists是對外表作loop,每次loop再對內表進行查詢。
一直以來認為exists比in效率高的說法是不準確的。
如果查詢的兩個表大小相當,那么用in和exists差別不大。
如果兩個表中一個較小,一個是大表,則子查詢表大的用exists,子查詢表小的用in:
例如:表A(小表),表B(大表)
1:
select * from A where cc in (select cc from B)
效率低,用到了A表上cc列的索引;
select * from A where exists(select cc from B where cc=*)
效率高,用到了B表上cc列的索引。
相反的
2:
select * from B where cc in (select cc from A)
效率高,用到了B表上cc列的索引;
select * from B where exists(select cc from A where cc=*)
效率低,用到了A表上cc列的索引。
帶in的關聯子查詢是多余的,因為in子句和子查詢中相關的操作的功能是一樣的。如:
select staff_name from staff_member where staff_id in
(select staff_id from staff_func where staff_*_id=staff_*_id);
為非關聯子查詢指定exists子句是不適當的,因為這樣會產生笛卡乘積。如:
select staff_name from staff_member where staff_id
exists (select staff_id from staff_func);
not in 和not exists
如果查詢語句使用了not in 那么內外表都進行全表掃描,沒有用到索引;
而not extsts 的子查詢依然能用到表上的索引。
所以無論哪個表大,用not exists都比not in要快。
盡量不要使用not in子句。使用minus 子句都比not in 子句快,雖然使用minus子句要進行兩次查詢:
select staff_name from staff_member where staff_id in (select staff_id from staff_member minus select staff_id from staff_func where func_id like '81%');
in 與 "=" 的區別
select name from student where name in ('zhang','wang','li','zhao');
與
select name from student where name='zhang' or name='li' or name='wang' or name='zhao'
的結果是相同的。
轉載請注明出處華閱文章網 » oracleinorin語句