oracle的exists⽤法总结
exists表⽰()内⼦查询语句返回结果不为空说明where条件成⽴就会执⾏主sql语句,如果为空就表⽰where条件不成⽴,sql语句就不会执⾏。not exists和exists相反,⼦查询语句结果为空,则表⽰where条件成⽴,执⾏sql语句。负责不执⾏。
之前在学oracle数据库的时候,接触过exists,做过⼏个简单的例⼦,,如
1.如果部门名称中含有字母A,则查询所有员⼯信息(使⽤exists)
select * from emp where exists (select * from dept where dname like '%A%' and deptno = emp.deptno) temp and
deptno=temp.deptno;
结果为:
exists的用法EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12⽉-80 800 20
7499 ALLEN SALESMAN 7698 20-2⽉ -81 1600 300 30
7521 WARD SALESMAN 7698 22-2⽉ -81 1250 500 30
7566 JONES MANAGER 7839 02-4⽉ -81 2975 20
7654 MARTIN SALESMAN 7698 28-9⽉ -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5⽉ -81 2850 30
7782 CLARK MANAGER 7839 09-6⽉ -81 2450 10
7788 SCOTT ANALYST 7566 19-4⽉ -87 3000 20
7839 KING PRESIDENT 17-11⽉-81 5000 10
7844 TURNER SALESMAN 7698 08-9⽉ -81 1500 0 30
7876 ADAMS CLERK 7788 23-5⽉ -87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7900 JAMES CLERK 7698 03-12⽉-81 950 30
7902 FORD ANALYST 7566 03-12⽉-81 3000 20
7934 MILLER CLERK 7782 23-1⽉ -82 1300 10
已选择14⾏。
2.如果有平均⼯资不⼩于1500的部门信息则查询所有部门信息(使⽤not exists)
select * from dept where not exists (select deptno from emp where deptno = emp.deptno group by deptno having avg(sal) < 1500) and exists (select * from emp where emp.deptno = deptno);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论