mysql、postgresql 集合union、intersect、except 操作

创建测试数据

-- 创建测试表
create table test11(
	name int
);

-- 插入数据
insert into test11 values(1),(2),(3),(4),(5),(6),(7),(8),(9);

一. 查询1 union 查询2

union表示并集,返回查询1和查询2查询到的所有数据,并且会过滤掉重复数据,可以使用union all不删除重复数据

  1. 执行sql和结果
select * from test11 where name <=5
union
select * from test11 where name > 7;

在这里插入图片描述

二. 查询1 intersect 查询2

intersect表示并集,返回查询1中有并且查询2中也有的数据,并且过滤重复数据,可以使用intersect all不过滤数据

  1. 执行sql和结果
select * from test11 where name <=5
intersect
select * from test11 where name >= 4;

在这里插入图片描述

三. 查询1 except 查询2

except表示差集,返回查询2中没有的查询1中数据,也就是不能返回查询2中有的数据,一样会过滤重复行,可以使用except all不过滤重复行

  1. 执行sql和结果
select * from test11
except
select * from test11 where name >= 4;

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值