SQL Server 有关EXCEPT和INTERSECT使用
2015-06-20 16:14
435 查看
熟练使用SQL Server各种使用会带来多大的方便查询。今天介绍EXCEPT和INTERSECT。请注意,这只是语法SQL Server 2005和以上版本支持。
EXCEPT它指的是存在于所述第一组。但它不存在于所述第二组数据的。
INTERSECT是指在两个集合中都存在的数据。
測试例如以下:
EXCEPT和INTERSECT的优先级:
为了測试它们之间的优先级,执行以下的測试代码:
为什么会出现如上结果呢,请看以下的运行计划:
原来t2和t3先进行的INTERSECT手术,获得5 m5结果集,一次t1进行EXCEPT手术。
如需转载,请注明来自原来的文章CSDN TJVictor专栏:http://blog.csdn.net/tjvictor
EXCEPT它指的是存在于所述第一组。但它不存在于所述第二组数据的。
INTERSECT是指在两个集合中都存在的数据。
測试例如以下:
create table t1(id int,mark char(2)) go create table t2(id int,mark char(2)) go insert into t1 select 1,'t1' union all select 2,'t2' union all select 3,'t3' union all select 4,'t4' go insert into t2 select 2,'t2' union all select 3,'m3' union all select 5,'m5' union all select 6,'t6' go select * from t1 EXCEPT select * from t2 go select * from t1 INTERSECT select * from t2 go --EXCEPT结果集为 --1 t1 --3 t3 --4 t4 --INTERSECT结果集为 --2 t2
EXCEPT和INTERSECT的优先级:
为了測试它们之间的优先级,执行以下的測试代码:
create table t3(int id,mark char(2)) go insert into t3 select 3,'t3' union all select 3,'r3' union all select 5,'m5' union all select 5,'r5' union all select 7,'b7' union all select 8,'b8' go select * from t1 EXCEPT select * from t2 INTERSECT select * from t3 --执行结果 --1 t1 --2 t2 --3 t3 --4 t4
为什么会出现如上结果呢,请看以下的运行计划:
原来t2和t3先进行的INTERSECT手术,获得5 m5结果集,一次t1进行EXCEPT手术。
如需转载,请注明来自原来的文章CSDN TJVictor专栏:http://blog.csdn.net/tjvictor
相关文章推荐
- 安装memcached扩展 验证过了可行
- Redis容量及使用规划(转)
- 在一个千万级别的数据库中查询,如何提高查询效率
- MySQL: Speed of INSERT Statements
- 在scala中访问postgresql(使用sbt)
- 5.MyBatis 全局配置文件详解(sqlMapConfig.xml)
- mysql主键约束和唯一性约束
- 利用SQL语句查找姓“张”且分数大于80的记录(某公司招聘笔试试题)
- Windows 下Oracle数据库安装以及Oracle SQL Developer(图文详解)
- Mysql常用命令行
- [Training Video - 7] [Database connection] Various databases which are supported, Drivers for database connection, SQL Groovy API
- MySQL数据库远程连接开启方法
- redis主从的配置和使用
- com.mysql.jdbc.Driver和org.gjt.mm.mysql.Driver区别
- MySQL常用命令
- 【安德鲁斯】基于脚本的数据库"增量更新",如果不改变,每次更新java代码、!
- 组态ORACLE 11G ADG
- Windows 2008 + SQLServer 2008 双机群集
- Sql Server 中非常强大的日期格式化函数
- 【转】【mysql的设计与优化专题】mysql的最佳索引攻略