【SQL】Oracle的交集、并集、差集

假设有表tableA、tableB,他们都有字段id和name

交集:INTERSECT (适用于两个结果集)

select a.id, a.name from tableA a
INTERSECT
select b.id, b.name from tableB b

差集:MINUS(适用于两个结果集)

select a.id, a.name from tableA a
MINUS
select b.id, b.name from tableB b

并集:UNION(适用于两个结果集)

-- UNION、UNION ALL能对两个或以上的结果集进行合并
-- 不包括重复行,进行默认排序
select a.id, a.name from tableA a
UNION
select b.id, b.name from tableB b

-- 包括重复行,不进行默认排序
select a.id, a.name from tableA a
UNION ALL
select b.id, b.name from tableB b

猜你喜欢

转载自www.cnblogs.com/tubybassoon/p/9687262.html