oracle exists and not exist
oracle exists and not exist
ORACLE 2007-08-30 11:06:08 閱讀6221 評論1 字號:大中小 訂閱
先看下面的例子:oracle中兩個系統表.emp,dept.
example:
1:not exists(not in)
not exists:
這條語句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10條件滿足的結果集.也就是說,
返回的結果集中不存在d.deptno=10結果集的記錄,即emp表中沒有dept表中d.deptno=10的記錄.
SQL> select empno,ename,deptno from scott.emp e where not exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);
EMPNO ENAME DEPTNO
----- ---------- ------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7788 SCOTT 20
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
11 rows selected
not in:
第一個where條件必須給定欄位(deptno), 第二個sql語句中,必須明確給定要查詢的欄位是哪個(deptno).
SQL> select empno,ename,deptno from scott.emp e where deptno not in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);
EMPNO ENAME DEPTNO
----- ---------- ------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7788 SCOTT 20
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
11 rows selected
2: exists(in)
exists:
這條語句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10條件滿足的記錄結果集.
也就是說返回的結果集中只存在有d.deptno=10的記錄,即emp表中只存在dept表中d.deptno=10的記錄.
SQL> select empno,ename,deptno from scott.emp e where exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);
EMPNO ENAME DEPTNO
----- ---------- ------
7782 CLARK 10
7839 KING 10
7934 MILLER 10
in:
第一個where條件必須給定欄位(deptno), 第二個sql語句中,必須明確給定要查詢的欄位是哪個(deptno).
SQL> select empno,ename,deptno from scott.emp e where deptno in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);
EMPNO ENAME DEPTNO
----- ---------- ------
7782 CLARK 10
7839 KING 10
7934 MILLER 10
oracle 中exists (in)和not exists(not in)是判斷是否存在和不存在表中記錄的關鍵子.
請注意:not in 邏輯上不完全等同於not exists,如果你誤用了not in,小心你的程式存在致命的BUG:因此,請儘量不要使用not in(它會呼叫子查詢),而儘量使用not exists(它會呼叫關聯子查詢)。
ORACLE 2007-08-30 11:06:08 閱讀6221 評論1 字號:大中小 訂閱
先看下面的例子:oracle中兩個系統表.emp,dept.
example:
1:not exists(not in)
not exists:
這條語句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10條件滿足的結果集.也就是說,
返回的結果集中不存在d.deptno=10結果集的記錄,即emp表中沒有dept表中d.deptno=10的記錄.
SQL> select empno,ename,deptno from scott.emp e where not exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);
EMPNO ENAME DEPTNO
----- ---------- ------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7788 SCOTT 20
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
11 rows selected
not in:
第一個where條件必須給定欄位(deptno), 第二個sql語句中,必須明確給定要查詢的欄位是哪個(deptno).
SQL> select empno,ename,deptno from scott.emp e where deptno not in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);
EMPNO ENAME DEPTNO
----- ---------- ------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7788 SCOTT 20
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
11 rows selected
2: exists(in)
exists:
這條語句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10條件滿足的記錄結果集.
也就是說返回的結果集中只存在有d.deptno=10的記錄,即emp表中只存在dept表中d.deptno=10的記錄.
SQL> select empno,ename,deptno from scott.emp e where exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);
EMPNO ENAME DEPTNO
----- ---------- ------
7782 CLARK 10
7839 KING 10
7934 MILLER 10
in:
第一個where條件必須給定欄位(deptno), 第二個sql語句中,必須明確給定要查詢的欄位是哪個(deptno).
SQL> select empno,ename,deptno from scott.emp e where deptno in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);
EMPNO ENAME DEPTNO
----- ---------- ------
7782 CLARK 10
7839 KING 10
7934 MILLER 10
oracle 中exists (in)和not exists(not in)是判斷是否存在和不存在表中記錄的關鍵子.
請注意:not in 邏輯上不完全等同於not exists,如果你誤用了not in,小心你的程式存在致命的BUG:因此,請儘量不要使用not in(它會呼叫子查詢),而儘量使用not exists(它會呼叫關聯子查詢)。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25897606/viewspace-712510/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle中exists和in的效能差異Oracle
- EXISTS、IN、NOT EXISTS、NOT IN(zt)
- [20180808]exists and not exists.txt
- sql:delete if exists還是drop if exists?SQLdelete
- exists()、not exists() 、in()、not in()用法以及效率差異
- Oracle優化案例-用left join代替反連線 not in not exists(十)Oracle優化
- oracle之優化一用group by或exists優化distinctOracle優化
- 靜默安裝Oracle建庫時報Template General Purpose does not existOracle
- In和exists使用及效能分析(二):exists的使用
- in、exists與索引索引
- SQL not exist out joinSQL
- Oralce 使用SQL中的exists 和not exists 用法詳解SQL
- In和exists使用及效能分析(三):in和exists的效能分析
- elasticsearch之exists查詢Elasticsearch
- Waring: /dev/centos/swap does not existdevCentOS
- sql case when, Exist ,group by ,聚合SQL
- Property [title] does not exist on this collection instance
- PSQLexception: ERROR : type "signed" does not existSQLExceptionError
- [精選] SQL中的IN與NOT IN、EXISTS與NOT EXISTS的區別及效能分析SQL
- mysql 關於exists 和in分析MySql
- [20180928]exists與cardinality.txt
- not in 和 not exists 比較和用法
- 對線面試官:SQL中的IN與NOT IN、EXISTS與NOT EXISTS的區別及效能分析面試SQL
- exists與in子查詢優化優化
- 【原始碼】Redis exists命令bug分析原始碼Redis
- beego報錯 table name: `xxx` not existsGo
- SQL語句中not in 和not exist的區別SQL
- Property 'context' does not exist on type 'NodeRequire'.ts(2339)ContextUI
- Laravel Class env does not exist 問題排查Laravel
- Elasticsearch Java High Level REST Client(Exists API)ElasticsearchJavaRESTclientAPI
- 【MySQL】NOT EXISTS優化的一個案例MySql優化
- Python BUG FileExistsError: [Errno 17] File exists: xxxPythonError
- 關於hibernate的 No row with the given identifier existsIDE
- mybatis exists 中使用代替in關鍵字MyBatis
- SQL語句中exists和in的區別SQL
- template might not exist or might not be accessible by any of the configured Template Resolvers
- 115 svn: URL 'svn://132.232.108.38/think' doesn't exist
- Setup had an error Error: At least one of these paths should existErrorAST
- ORA-04043: object DBA_DATA_FILES does not existObject