创建测试数据
-- 创建测试表
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不删除重复数据
- 执行sql和结果
select * from test11 where name <=5
union
select * from test11 where name > 7;
二. 查询1 intersect 查询2
intersect表示并集,返回查询1中有并且查询2中也有的数据,并且过滤重复数据,可以使用intersect all不过滤数据
- 执行sql和结果
select * from test11 where name <=5
intersect
select * from test11 where name >= 4;
三. 查询1 except 查询2
except表示差集,返回查询2中没有的查询1中数据,也就是不能返回查询2中有的数据,一样会过滤重复行,可以使用except all不过滤重复行
- 执行sql和结果
select * from test11
except
select * from test11 where name >= 4;