【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