ORACLE 交、并、差集用法

开发工具与关键技术:Oracle sql*plus PLSQL Developer
作者:华境聪
撰写时间:2019年03月26日

首先说明下用到的函数具体定义
intersect: 交集
union :并集去重复项
union ALL:并集不去重复
minus :差集(两个相同抵消,不同值的话一律返回上面的那个)

具体实践通过以下例子展示 :
题目:计算机系”与“电子工程系“不同职称的教师的Tname和Prof。?
方法一:通过交、并、差、得到自己想要的数据 (A∪B) - (A∩B)
select a.tname,a.prof
from teacher a join
((select prof
from teacher
where depary like ‘计算机系’
union
select prof
from teacher
where depary like ‘电子工程系’)minus
(select prof
from teacher
where depary like ‘计算机系’
intersect
select prof
from teacher
where depary like ‘电子工程系’)) b
on a.prof = b.prof

方法二:通过表连接,分组获取想要的数据
select a.Tname,a.Prof
from teacher a join (select Prof
from teacher
where depary like ‘计算机系’ or depary like ‘电子工程系’
group by Prof
having count(*) = 1)b on a.prof = b.Prof
输出结果截图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41979469/article/details/88900550