【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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM