【Oracle&SQLServer】並集、交際、補集


1、並集(UNION/UNION ALL)

Oracle&SQLServer中用法一致

UNION 去重
UNION ALL 不去重

 

-- 去重
select * from tablea
union
select * from tableb

-- 不去重
select * from tablea
union all
select * from tableb

 

 2、交集(INTERSECT/EXISTS)

Oracle&SQLServer中用法一致

INTERSECT 去重
EXISTS 不去重

 

-- 去重
select * from tablea
intersect
select * from tableb

-- 不去重
select * from tablea a
where exists (select 1 from tableb b where a.ID=b.ID)

 

3、補集(MINUS/EXCEPT/NOT EXISTS)

Oracle:

MINUS 去重
NOT EXISTS 不去重

 

 

SQLServer:

EXCEPT 去重
NOT EXISTS 不去重

 

 

-- Oracle去重
select * from tablea
minus
select * from tableb

-- SQLServer去重
select * from tablea
except
select * from tableb

-- Oracle&SQLServer不去重
select * from tablea a
where not exists (select 1 from tableb b where a.ID=b.ID)

 


免責聲明!

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



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