MINUS(差集)、INTERSECT(交集)和UNION (ALL)(并集)

MINUS(差集)

代码:

WITH A AS (
SELECT '1' AS num FROM dual UNION
SELECT '2' AS num FROM dual UNION
SELECT '3' AS num FROM dual UNION
SELECT '4' AS num FROM dual UNION
SELECT '5' AS num FROM dual UNION
SELECT '6' AS num FROM dual UNION
SELECT '7' AS num FROM dual UNION
SELECT '8' AS num FROM dual UNION
SELECT '9' AS num FROM dual ), B AS (
SELECT '6' AS num FROM dual UNION
SELECT '7' AS num FROM dual UNION
SELECT '8' AS num FROM dual UNION
SELECT '9' AS num FROM dual UNION
SELECT '10' AS num FROM dual UNION
SELECT '11' AS num FROM dual UNION
SELECT '12' AS num FROM dual UNION
SELECT '13' AS num FROM dual UNION
SELECT '14' AS num FROM dual UNION
SELECT '15' AS num FROM dual UNION
SELECT '16' AS num FROM dual)
SELECT num FROM A
MINUS
SELECT num FROM B;

运行结果如下图所示:




INTERSECT(交集)



代码:

WITH A AS (
SELECT '1' AS num FROM dual UNION
SELECT '2' AS num FROM dual UNION
SELECT '3' AS num FROM dual UNION
SELECT '4' AS num FROM dual UNION
SELECT '5' AS num FROM dual UNION
SELECT '6' AS num FROM dual UNION
SELECT '7' AS num FROM dual UNION
SELECT '8' AS num FROM dual UNION
SELECT '9' AS num FROM dual ), B AS (
SELECT '6' AS num FROM dual UNION
SELECT '7' AS num FROM dual UNION
SELECT '8' AS num FROM dual UNION
SELECT '9' AS num FROM dual UNION
SELECT '10' AS num FROM dual UNION
SELECT '11' AS num FROM dual UNION
SELECT '12' AS num FROM dual UNION
SELECT '13' AS num FROM dual UNION
SELECT '14' AS num FROM dual UNION
SELECT '15' AS num FROM dual UNION
SELECT '16' AS num FROM dual)
SELECT num FROM A
INTERSECT
SELECT num FROM B;

运行结果如下图所示:



UNION (ALL)(并集)------UNION



代码:

WITH A AS (
SELECT '1' AS num FROM dual UNION
SELECT '2' AS num FROM dual UNION
SELECT '3' AS num FROM dual UNION
SELECT '4' AS num FROM dual UNION
SELECT '5' AS num FROM dual UNION
SELECT '6' AS num FROM dual UNION
SELECT '7' AS num FROM dual UNION
SELECT '8' AS num FROM dual UNION
SELECT '9' AS num FROM dual ), B AS (
SELECT '6' AS num FROM dual UNION
SELECT '7' AS num FROM dual UNION
SELECT '8' AS num FROM dual UNION
SELECT '9' AS num FROM dual UNION
SELECT '10' AS num FROM dual UNION
SELECT '11' AS num FROM dual UNION
SELECT '12' AS num FROM dual UNION
SELECT '13' AS num FROM dual UNION
SELECT '14' AS num FROM dual UNION
SELECT '15' AS num FROM dual UNION
SELECT '16' AS num FROM dual)
SELECT num FROM A
UNION
SELECT num FROM B;

运行结果如下图所示:



UNION (ALL)(并集)------UNION ALL


代码:

WITH A AS (
SELECT '1' AS num FROM dual UNION
SELECT '2' AS num FROM dual UNION
SELECT '3' AS num FROM dual UNION
SELECT '4' AS num FROM dual UNION
SELECT '5' AS num FROM dual UNION
SELECT '6' AS num FROM dual UNION
SELECT '7' AS num FROM dual UNION
SELECT '8' AS num FROM dual UNION
SELECT '9' AS num FROM dual ), B AS (
SELECT '6' AS num FROM dual UNION
SELECT '7' AS num FROM dual UNION
SELECT '8' AS num FROM dual UNION
SELECT '9' AS num FROM dual UNION
SELECT '10' AS num FROM dual UNION
SELECT '11' AS num FROM dual UNION
SELECT '12' AS num FROM dual UNION
SELECT '13' AS num FROM dual UNION
SELECT '14' AS num FROM dual UNION
SELECT '15' AS num FROM dual UNION
SELECT '16' AS num FROM dual)
SELECT num FROM A
UNION ALL
SELECT num FROM B;

运行结果如下图所示:



PS:
应用两个集合MINUS,INTERSECT,UNION(ALL)时,是有严格要求的
№_01: 两个集合的字段必须明确(用*就不行会产生报错);
№_02: 字段类型和顺序相同(名称可以不同)。如:集合A的字段1是NUMBER型,字段2是VARCHAR型,那么集合B的字段1也必须是NUMBER型,字段2也必须是VARCHAR型;
№_03: 不能排序。如果要对结果排序,可以在集合运算后,外面再套一个查询,然后排序。




转载于:https://www.cnblogs.com/veygoux/p/4181503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值