用户scott下
emp表:
[img]http://dl.iteye.com/upload/attachment/211270/190afcb5-7e8f-325e-a66f-aa94929bbe57.png[/img]
emp表:
[img]http://dl.iteye.com/upload/attachment/211272/4ba63553-010f-32a8-a606-3f82107522f6.png[/img]
oracle集合函数包括:minus(差集),union(并集),union all(不排重并集),intersect(交集)
1.minus:找到两个给定的数值集合之间的差异,意味着找到一个数值集合,其中的元素仅存在于前一个集合中,而不存在于另一个集合中。
[img]http://dl.iteye.com/upload/attachment/211278/52c0ea3d-8d1a-3411-a28c-6bc875cbe41b.png[/img]
分析:查相同列名的数据,查出的40,只存在与前一张表,即dept表中。
2.union:连接查询的两张表中相同列名的数据的并集。
[img]http://dl.iteye.com/upload/attachment/211281/c130110e-2abd-326e-80d0-f86f1d4c195b.png[/img]
分析:查出2张表中的并集。
3.union all:连接查询两张表中相同列名的所有数据。(与union 不同的是它不去除重复数据)。
[img]http://dl.iteye.com/upload/attachment/211283/13f449d4-cffa-3b0e-8c2f-ecb06e2aab11.png[/img]
分析:查出2张表中所有的值。
4.intersect:查询intersect连接的两张表中相同列名的数据,查出交集的数据。
[img]http://dl.iteye.com/upload/attachment/211285/a71b4e08-6dce-3404-bf9c-179fd7376c30.png[/img]
分析:查出2张表中所有的交集。
5.举个例子说明集合运算的应用:
查询出没有员工的那个部门的部门编号和部门名称.
(1).平常做法:
[img]http://dl.iteye.com/upload/attachment/211294/f33d4c4f-2d57-3433-a677-b5d5bd886563.png[/img]
(2).使用minus做法:
[img]http://dl.iteye.com/upload/attachment/211294/f33d4c4f-2d57-3433-a677-b5d5bd886563.png[/img]
结果:(一样)
emp表:
select deptno from dept
[img]http://dl.iteye.com/upload/attachment/211270/190afcb5-7e8f-325e-a66f-aa94929bbe57.png[/img]
emp表:
select deptno from emp
[img]http://dl.iteye.com/upload/attachment/211272/4ba63553-010f-32a8-a606-3f82107522f6.png[/img]
oracle集合函数包括:minus(差集),union(并集),union all(不排重并集),intersect(交集)
1.minus:找到两个给定的数值集合之间的差异,意味着找到一个数值集合,其中的元素仅存在于前一个集合中,而不存在于另一个集合中。
select deptno from dept
minus select
deptno from emp
[img]http://dl.iteye.com/upload/attachment/211278/52c0ea3d-8d1a-3411-a28c-6bc875cbe41b.png[/img]
分析:查相同列名的数据,查出的40,只存在与前一张表,即dept表中。
2.union:连接查询的两张表中相同列名的数据的并集。
select deptno from dept
union
select deptno from emp
[img]http://dl.iteye.com/upload/attachment/211281/c130110e-2abd-326e-80d0-f86f1d4c195b.png[/img]
分析:查出2张表中的并集。
3.union all:连接查询两张表中相同列名的所有数据。(与union 不同的是它不去除重复数据)。
select deptno from dept
union all
select deptno from emp
[img]http://dl.iteye.com/upload/attachment/211283/13f449d4-cffa-3b0e-8c2f-ecb06e2aab11.png[/img]
分析:查出2张表中所有的值。
4.intersect:查询intersect连接的两张表中相同列名的数据,查出交集的数据。
select deptno from dept
intersect
select deptno from emp
[img]http://dl.iteye.com/upload/attachment/211285/a71b4e08-6dce-3404-bf9c-179fd7376c30.png[/img]
分析:查出2张表中所有的交集。
5.举个例子说明集合运算的应用:
查询出没有员工的那个部门的部门编号和部门名称.
(1).平常做法:
select deptno,dname from dept
where deptno not in
(select distinct (deptno) from emp)
[img]http://dl.iteye.com/upload/attachment/211294/f33d4c4f-2d57-3433-a677-b5d5bd886563.png[/img]
(2).使用minus做法:
select deptno,dname from dept
where deptno in
(select deptno from dept
minus
select deptno from emp
)
[img]http://dl.iteye.com/upload/attachment/211294/f33d4c4f-2d57-3433-a677-b5d5bd886563.png[/img]
结果:(一样)