Oracle-SET运算符/查询的并交差

本文详细介绍了SQL中的集合操作包括union/unionall、intersect和minus的功能及用法,并通过具体示例展示了如何使用这些操作来处理数据,帮助读者理解并掌握这些重要的SQL特性。

1.union/union all

1.必须具有对应的列数以及数据类型

2.默认按照第一列从小到大的顺序排列

3.union 去重且排序;

4.union all 不去重且不拍序。

--employees01 1-70,33-80;employees01 3-90,33-80
【employee01有70号部门1人,80号部门3人,一下类似】

--查找去重之后两表的并集-union-37
/*
select employee_id,department_id
from employees01
union
select employee_id,department_id
from employees02
*/
--查找去重之前两表的并集-union-70
/*
select employee_id,department_id
from employees01
union all
select employee_id,department_id
from employees02
*/

2.intersect

--取二者的交集-intersect-33-80
/*
select employee_id,department_id
from employees01
intersect
select employee_id,department_id
from employees02
*/

3.minus

前表-二者共有的

--取二者的差集-minus-1-70
/*
select employee_id,department_id
from employees01
minus
select employee_id,department_id
from employees02

*/

4.使用相对位置进行排序

--查询10,50,,20号部门的job_id,department_id 并且 department_id按10,50,20的顺序排列
/*
select job_id,department_id,1 from employees where department_id = 10
union
select job_id,department_id,2 from employees where department_id = 50
union
select job_id,department_id,3 from employees where department_id = 20
order by 3 asc

*/
--column a_dummy noprin; 不打印该别名对应的列

--查询所有员工的last_name,department_id,department_name
/*
select last_name,department_id,to_char(null) from employees
union
select to_char(null),department_id,department_name from departments
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值