【查询】集合的交、并、差集运算(intersect,union,except)

SQLServer中通过intersect,union,except和三个关键字对应交、并、差三种集合运算。

他们的对应关系可以参考下面图示:

 

测试示例:

构造A,B两个数据集

 

查询示例:

1.Union 取合集并过滤重复数据

 

2.Union all 取合集不过滤重复数据

 

3.Intersect 取交集(两个表中都有数据)

 

4. except 取差集(取A-B的记录)

 

参考资料:

https://blog.csdn.net/lqh4188/article/details/47144101

 

 

 

 

 

posted @ 2019-12-12 16:33  黑涩小豆  阅读(703)  评论(0)    收藏  举报