合集
1)union
部门是ACCOUNTING和工种是CLERK的人员集合
SQL> select e.empno,ename,d.dname,e.job from emp e,dept d where e.deptno=d.deptno and d.dname='ACCOUNTING' union select e.empno,ename,d.dname,e.job from emp e,dept d where e.deptno=d.deptno and e.job='CLERK';
EMPNO ENAME DNAME JOB
---------- ---------- -------------- ---------
7369 SMITH RESEARCH CLERK
7782 CLARK ACCOUNTING MANAGER
7839 KING ACCOUNTING PRESIDENT
7876 ADAMS RESEARCH CLERK
7900 JAMES SALES CLERK
7934 MILLER ACCOUNTING CLERK
6 rows selected.
注意:集合交叉部分不会重复计算。
2)union all
SQL> select e.empno,ename,d.dname,e.job from emp e,dept d where e.deptno=d.deptno and d.dname='ACCOUNTING' union all select e.empno,ename,d.dname,e.job from emp e,dept d where e.deptno=d.deptno and e.job='CLERK';
EMPNO ENAME DNAME JOB
---------- ---------- -------------- ---------
7782 CLARK ACCOUNTING MANAGER
7839 KING ACCOUNTING PRESIDENT
7934 MILLER ACCOUNTING CLERK
7934 MILLER ACCOUNTING CLERK
7369 SMITH RESEARCH CLERK
7876 ADAMS RESEARCH CLERK
7900 JAMES SALES CLERK
7 rows selected.
注意:集合交叉部分会重复计算。
交集
intersect
部门是ACCOUNTING工种是CLERK的人员
SQL> select e.empno,ename,d.dname,e.job from emp e,dept d where e.deptno=d.deptno and d.dname='ACCOUNTING' intersect select e.empno,ename,d.dname,e.job from emp e,dept d where e.deptno=d.deptno and e.job='CLERK';
EMPNO ENAME DNAME JOB
---------- ---------- -------------- ---------
7934 MILLER ACCOUNTING CLERK
差集
minus
部门是ACCOUNTING工种不是CLERK的人员
select e.empno,ename,d.dname,e.job from emp e,dept d where e.deptno=d.deptno and d.dname='ACCOUNTING' minus select e.empno,ename,d.dname,e.job from emp e,dept d where e.deptno=d.deptno and e.job='CLERK';
EMPNO ENAME DNAME JOB
---------- ---------- -------------- ---------
7782 CLARK ACCOUNTING MANAGER
7839 KING ACCOUNTING PRESIDENT
注意在使用集合操作时所有select选出来的column必须完全一致如:都是e.empno,ename,d.dname,e.job。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20844861/viewspace-594626/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/20844861/viewspace-594626/