牛骨文教育服务平台(让学习变的简单)
博文笔记

SQL集合运算:差集、交集、并集

创建时间:2011-03-30 投稿人: 浏览次数:14865

1、差集( except )

select a from t_a

except

select a from t_b

 

-- 也可写作:

select a from t_a where a not in (select a from t_b)

 

-- 多个字段时:

select a,b from t_a

except

select a,b from t_b

 

-- 多字段的查集也可写成:

select a,b from t_a where (a,b) not in (select a,b from t_b)

 

2、交集( intersect )

select a from t_a

intersect

select a from t_b

 

-- 也可写作:

   select a from t_a where a in (select a from t_b)

 

3、并集( union )

select a from t_a

union distinct

select a from t_b

 

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。