Oracle的集合操作


关键字:

1、UNION:并集,所有的内容都查询,重复的显示一次;

2、UNION ALL:并集,所有的内容都显示,包括重复的;

3、INTERSECT:交集,只显示重复的;

4、MINUS:差集,只显示对方没有的(跟顺序是有关系的)

例子:

在scott用户下,创建表emp2,该表只包含emp中20部门员工的信息:

代码:create table emp2 as select * from emp where deptno=20;

先看下emp和emp2两个表的区别:

[emp表结构及内容]

 

[emp2表结构及内容]

~ 验证UNION及UNION ALL

 UNION:select * from emp UNION select * from emp2;/*使用此语句,重复的内容不再显示*/

 

 UNION ALL:select * from emp UNION ALL select * from emp2;/*使用此语句,重复的内容依然显示*/

~ 验证INTERSECT

 INTERSECT:select * from emp INTERSECT select * from emp2;/*使用此语句,只显示两个表中彼此重复的记录*/


~ 验证MINUS

 MINUS:select * from emp MINUS select * from emp2;/*使用此语句,返回显示差异的记录*/

相关内容