今天一个开发问到一个not exists问题,小小做个实验
实验1
SQL> select * from emp e1 where not exists (select 1 from emp e2 where e1.mgr = e2.empno);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------ ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-81 5000 10
实验2
SQL> select * from emp e1 where not exists (select 1 from emp e2 where e1.empno = e2.mgr);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------ ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
8 rows selected.
1、2 SQL大致一样,出现的结果却完全不一样
实验1是这里有多少人没有manager
实验2是这里多少人存在manager
原文地址:http://blog.csdn.net/ora_raymond/article/details/36008059