数据库简单查询——查询集合运算

查询集合运算

关键字: 并集: union, 全并集: union all, 交集: intersect, 差集: minus
比如说有集合 A ( 1 , 2 , 3 ) , B ( 2 , 3 , 4 ):
A∪B(取并集) = ( 1, 2, 3, 4) ,如果是全并集就是 (1, 2, 3, 2, 3, 4)
A∩B (取交集) = ( 2, 3 )
A – B(取差集) = ( 1 ) ,B – A = (4)

1、并集

例:部门为10以及部门为20的员工信息的并集
select * from emp where deptno=10
union
select * from emp where deptno=20;

2、全并集

例如:部门号为10和10或20的员工信息的并集
select * from B where deptno=10
union all
select * from B where deptno in(10,20);

3、交集

例如:部门号为10和部门为10或20的员工信息的交集
select * from B where deptno=10
intersect
select * from B where deptno in(10,20);

4、差集

例如:部门号为10或30与部门号为10或20的员工信息的差集
select * from B where deptno in(10, 30)
minus
select * from B where deptno in(10,20);

注意
参与运算的各个集合必须列数相同,且类型一致
采用第一个集合作为最后的表头(如果要写别名也在第一个语句上写)
order by永远在最后
可以使用括号改变执行顺序
如果列数不一样使用to_char(null)或to_number(null)代替:
例如:
select deptno,sum(salary) from B group by deptno,job
union
select deptno,to_char(null),sum(sal) form B group by deptno;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值