Home Database Mysql Tutorial Oracle exists and not exist

Oracle exists and not exist

Jun 07, 2016 pm 04:58 PM
oracle

先看下面的例子:oracle中两个系统表.emp,dept.example:1:not exists(not in)not exists: 这条语句返回select * from scott.dep

先看下面的例子: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(它会调用关联子查询)。

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Function to calculate the number of days between two dates in oracle Function to calculate the number of days between two dates in oracle May 08, 2024 pm 07:45 PM

Function to calculate the number of days between two dates in oracle

How long will Oracle database logs be kept? How long will Oracle database logs be kept? May 10, 2024 am 03:27 AM

How long will Oracle database logs be kept?

The order of the oracle database startup steps is The order of the oracle database startup steps is May 10, 2024 am 01:48 AM

The order of the oracle database startup steps is

How to use interval in oracle How to use interval in oracle May 08, 2024 pm 07:54 PM

How to use interval in oracle

Oracle database server hardware configuration requirements Oracle database server hardware configuration requirements May 10, 2024 am 04:00 AM

Oracle database server hardware configuration requirements

How to see the number of occurrences of a certain character in Oracle How to see the number of occurrences of a certain character in Oracle May 09, 2024 pm 09:33 PM

How to see the number of occurrences of a certain character in Oracle

How to determine whether two strings are contained in oracle How to determine whether two strings are contained in oracle May 08, 2024 pm 07:00 PM

How to determine whether two strings are contained in oracle

How much memory does oracle require? How much memory does oracle require? May 10, 2024 am 04:12 AM

How much memory does oracle require?

See all articles