union 、 union all
来源:互联网 发布:网络入侵检测系统 编辑:程序博客网 时间:2024/06/05 02:24
查询所有教师和同学的name、sex和birthday.
select tname,tsex,tbirthday from teacher union select sname,ssex,sbirthday from student;
在数据库中,UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同。
select tname,tsex,tbirthday from teacher union select sname,ssex,sbirthday from student;
在数据库中,UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同。
UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果。
实际大部分应用中是不会产生重复的记录,最常见的是过程表与历史表UNION。如:
select * from gc_dfys
union
select * from ls_jg_dfys
这个SQL在运行时先取出两个表的结果,再用排序空间进行排序删除重复的记录,最后返回结果集,如果表数据量大的话可能会导致用磁盘进行排序。
排序是对所有记录进行排序,即使在一个查询中的重复数据也会被去掉
而UNION ALL只是简单的将两个结果合并后就返回。这样,如果返回的两个结果集中有重复的数据,那么返回的结果集就会包含重复的数据了。
从效率上说,UNION ALL 要比UNION快很多,所以,如果可以确认合并的两个结果集中不包含重复的数据的话,那么就使用UNION ALL,如下:
select * from gc_dfys
union all
select * from ls_jg_dfys
- union,union all 等
- union all 和union
- UNION,UNION ALL,MINUS
- union union all
- UNION and UNION All
- union 和 union all
- UNION与UNION ALL
- SQL Union / Union All
- UNION 和 UNION ALL
- union和union all
- union all和union
- union 、 union all
- union 、 union all
- Oracle union union all
- Union 与 Union ALl
- mysql UNION UNION All
- UNION和UNION ALL
- union and union all
- svn版本控制说明
- svn使用例子
- google地图开发总结(markerManager)
- solr日志级别
- mysql 批处理文件 创建 数据 导入数据
- union 、 union all
- iphone-common-codes-ccteam源代码 CCHttp.m
- 我下载编译导入Sipdroid2.1beta版本到eclipse的过程
- any 、all
- group by
- iphone-common-codes-ccteam源代码 CCIOS.h
- convert cast
- sql 语句练习
- iphone-common-codes-ccteam源代码 CCIOS.m