set操作符的使用

2021.10.31
set操作符

union

取并集,union仅包含一个表中的重复值,union all包含两表中的重复值

select employee_id emp_id, department_id dept_id
--可以在第一个表的列中写别名,第一个表中写了就不用写在第二个表中
from employees01
union
select employee_id,department_id
--两表内容需要一一对应
from employees02
order by emp_id

intersect

取交集

minus

取差集

相对位置

可以使用相对位置进行排序

select employee_id emp_id, department_id dept_id
--可以在第一个表的列中写别名,第一个表中写了就不用写在第二个表中
from employees01
union
select employee_id,department_id
--两表内容需要一一对应
from employees02
order by 1 --根据第一个列进行排序,即employee_id

排序顺序使用

select 'a' as 'column_name' from dual
union
select 'b' from dual
union
select 'c' from dual

会导致结果为bca
需要添加序号进行排序

select 'a' as 'column_name' ,1 a_dummy from dual
union
select 'b' ,2 from dual
union
select 'c' ,3 from dual
order by 2 asc--使用第二列进行排序

no print

添加SQL语句column a_dummy noprint
在命名为a_dummy后该列不会显示

确保列一一对应

添加null列保证能够一一对应

select employee_id emp_id,department_id,to_char(null)
from employees01
union
select to_number(null),department_id,department_name
from departments
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值