oracle中的 交集,并集,和差集

–查询系统中 的 emp表

select *
from emp;

– 涉及 并集 union(all) , 交集 intersect , 差集 minus

--查询 部门编号 为 1030 的所有信息

--or 和 union 类似于 并集
--第一种  使用 or
select*
from emp
where deptno = 10 or deptno=30 ;

– 第二种 使用 union all (联合所有结果集,不去重 )

select*
from emp
where deptno = 10     --这里语句未结束,不加分号
union all
select*
from emp
where deptno =30;

– 第三种 使用 union (联合两个结果集,去重)

select *
from emp
where deptno =10
union
select *
from emp
where deptno =30;

–并集 也可以 进行不同字段的 相交

select *
from emp
where deptno =10
union
select *
from emp
where empno =7844;

–intersect 交集 获取两个结果集 的 交集

select *
from emp
where deptno >=20 --这里查询语句 未结束

intersect 

select *
from emp
where deptno<30;  --结果仅为 编号为 20的所有信息

– 交集可以 进行 不同字段的 相交

select *
from emp
where deptno >=20 --这里查询语句 未结束,不加分号';'

intersect 

select *
from emp
where empno=7844;

–差集 minus

select *
from emp     
where deptno>10    --查询 编号大于10的(2030)     

minus     --查询语句1的结果集   -   查询语句2 的结果集
select *
from emp
where deptno=20;    --查询编号为 20的信息

–差集 语句1 的 和 语句 2 的字段 名不一样

select *
from emp     
where deptno>10    --查询 编号大于10的(2030)     

minus     --查询语句1的结果集 -   查询语句2 的结果集
select *
from emp
where empno=7876;   

– 若查询语句2 中的 部分结果 在 查询语句1 中 没有,那么 查询语句1 - 查询语句2 就会把 查询语句1 中 没有的 数据 清除

select *
from emp
where deptno=30 --编号为30 的信息

minus
select *
from emp
where deptno=20 or deptno =10  --编号为 2010的信息

–结果只剩下 编号为30的 所有信息

猜你喜欢

转载自blog.csdn.net/weixin_42785557/article/details/81738560