SQL语句中,union并集,intersect交集,except差集
注意此语法仅在SQL Server 2005及以上版本支持。
要求:表t1,t2的结构一致
t1中的数据:1,2,3,4,5
t2中的数据:3,4,5,6,7
UNION是指合并第一个集合,第二个集合中的数据。
- select * from t1
- UNON
- select * from t2
- 结果:1,2,3,4,5,6,7
UNION有重复记录只取一条,用UNION ALL 时取所有重复记录
- select * from t1
- UNON ALL
- select * from t2
- 结果:1,2,3,4,5,3,4,5,6,7
INTERSECT是指在两个集合中都存在的数据。
- select * from t1
- INTERSECT
- select * from t2
- 结果:3,4,5
EXCEPT是指在第一个集合中存在,但是不存在于第二个集合中的数据。
- select * from t1
- EXCEP
- select * from t2
- 结果:1,2
静守己心,看淡浮华