集合操作
用于多条select语句合并结果
union 并集 去重
union all 并集 不去重
intersect 交集
minus 差集
union
A集合和B集合的合并,但去掉两集合重复的部分 会排序
SCOTT@ora10g> select deptno,ename from emp where deptno in (20,30)
2 union
3 select deptno,ename from emp where deptno in (20,10)
4 ;
DEPTNO ENAME
---------- ----------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
SCOTT@ora10g>
union all
A集合和B集合的合并,不去重,不排序
SCOTT@ora10g> select deptno,ename from emp where deptno in (20,30)
2 union all
3 select deptno,ename from emp where deptno in (20,10)
4*
SCOTT@ora10g> /
DEPTNO ENAME
---------- ----------
20 SMITH
30 ALLEN
30 WARD
20 JONES
30 MARTIN
30 BLAKE
20 SCOTT
30 TURNER
20 ADAMS
30 JAMES
20 FORD
20 SMITH
20 JONES
10 CLARK
20 SCOTT
10 KING
20 ADAMS
20 FORD
10 MILLER
19 rows selected.
SCOTT@ora10g>
intersect
两个集合的交集部分,排序并去重
SCOTT@ora10g> select deptno,ename from emp where deptno in (20,30)
2 intersect
3 select deptno,ename from emp where deptno in (20,10)
4*
SCOTT@ora10g> /
DEPTNO ENAME
---------- ----------
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
SCOTT@ora10g>
minus
取两个集合的差集,A集合中存在,B集合中不存在的数据(取A集合中B集合不存在的数据) 去重
SCOTT@ora10g> select deptno,ename from emp where deptno in (20,30)
2 minus
3 select deptno,ename from emp where deptno in (20,10)
4*
SCOTT@ora10g>
DEPTNO ENAME
---------- ----------
30 ALLEN
30 BLAKE
30 JAMES
30 MARTIN
30 TURNER
30 WARD
6 rows selected.
SCOTT@ora10g>