Union和Union All的区别[转]
来源:http://blog.csdn.net/wanghai__/article/details/4712555/
假设我们有一个表Student,包括以下字段与数据:
- drop table student;
- create table student
- (
- id int primary key,
- name nvarchar2(50) not null,
- score number not null
- );
- insert into student values(1,'Aaron',78);
- insert into student values(2,'Bill',76);
- insert into student values(3,'Cindy',89);
- insert into student values(4,'Damon',90);
- insert into student values(5,'Ella',73);
- insert into student values(6,'Frado',61);
- insert into student values(7,'Gill',99);
- insert into student values(8,'Hellen',56);
- insert into student values(9,'Ivan',93);
- insert into student values(10,'Jay',90);
- commit;
首先,我们来看一下UNION的例子:
- SQL> select *
- 2 from student
- 3 where id<4
- 4 union
- 5 select *
- 6 from student
- 7 where id>2 and id<6
- 8 ;
- ID NAME SCORE
- ---------- ------------------------------ ----------
- 1 Aaron 78
- 2 Bill 76
- 3 Cindy 89
- 4 Damon 90
- 5 Ella 73
- SQL>
如果换成Union All连接两个结果集,则结果如下:
- SQL> select *
- 2 from student
- 3 where id<4
- 4 union all
- 5 select *
- 6 from student
- 7 where id>2 and id<6
- 8 ;
- ID NAME SCORE
- ---------- ------------------------------ ----------
- 1 Aaron 78
- 2 Bill 76
- 3 Cindy 89
- 3 Cindy 89
- 4 Damon 90
- 5 Ella 73
- 6 rows selected.
可以看到,Union和Union All的区别之一在于对重复结果的处理。
接下来,我们交换一个两个SELECT语句的顺序,看看结果是怎样的。
- SQL> select *
- 2 from student
- 3 where id>2 and id<6
- 4 union
- 5 select *
- 6 from student
- 7 where id<4
- 8 ;
- ID NAME SCORE
- ---------- ------------------------------ ----------
- 1 Aaron 78
- 2 Bill 76
- 3 Cindy 89
- 4 Damon 90
- 5 Ella 73
- SQL> select *
- 2 from student
- 3 where id>2 and id<6
- 4 union all
- 5 select *
- 6 from student
- 7 where id<4
- 8 ;
- ID NAME SCORE
- ---------- ------------------------------ ----------
- 3 Cindy 89
- 4 Damon 90
- 5 Ella 73
- 1 Aaron 78
- 2 Bill 76
- 3 Cindy 89
- 6 rows selected.
可以看到,对于UNION来说,交换两个SELECT语句的顺序后结果仍然是一样的,这是因为UNION会自动排序。而UNION ALL在交换了SELECT语句的顺序后结果则不相同,因为UNION ALL不会对结果自动进行排序。
那么这个自动排序的规则是什么呢?我们交换一下SELECT后面选择字段的顺序(前面使用SELECT *相当于SELECT ID,NAME,SCORE),看看结果如何:
- SQL> select score,id,name
- 2 from student
- 3 where id<4
- 4 union
- 5 select score,id,name
- 6 from student
- 7 where id>2 and id<6
- 8 ;
- SCORE ID NAME
- ---------- ---------- ------------------------------
- 73 5 Ella
- 76 2 Bill
- 78 1 Aaron
- 89 3 Cindy
- 90 4 Damon
可是看到,此时是按照字段SCORE来对结果进行排序的(前面SELECT *的时候是按照ID进行排序的)。
那么有人会问,如果我想自行控制排序,能不能使用ORDER BY呢?当然可以。不过在写法上有需要注意的地方:
- select score,id,name
- from student
- where id > 2 and id < 7
- union
- select score,id,name
- from student
- where id < 4
- union
- select score,id,name
- from student
- where id > 8
- order by id desc
order by子句必须写在最后一个结果集里,并且其排序规则将改变操作后的排序结果。对于Union、Union All、Intersect、Minus都有效。
其他的集合操作符,如Intersect和Minus的操作和Union基本一致,这里一起总结一下:
Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union All,对两个结果集进行并集操作,包括重复行,不进行排序;
Intersect,对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
Minus,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。
可以在最后一个结果集中指定Order by子句改变排序方式。
Union和Union All的区别[转]的更多相关文章
- Union和Union All到底有什么区别
以前一直不知道Union和Union All到底有什么区别,今天来好好的研究一下,网上查到的结果是下面这个样子,可是还是不是很理解,下面将自己亲自验证: Union:对两个结果集进行并集操作,不包括重 ...
- union和union all的区别
UNION 写一篇联合查询(把前后两个表的查询结果集合在前表中)首先有个为什么需要 相同记录数?? 记错了.应该是union两张表的查询字段数目要一致,字段类型要相似相同的数据类型,至少是相似,可转化 ...
- Oracle之Union与Union all的区别
如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并在一起显示出来. union和unio ...
- union与union all的区别
首先说下union与join的区别 1.union是以行增加的方式,进行连接:join是以列增加的方式进行连接: 2.union连接查询的两个表的字段必须要一一对应,数目相等:join则没有要求,但是 ...
- C和C++中结构体(struct)、联合体(union)、枚举(enum)的区别
C++对C语言的结构.联合.枚举 这3种数据类型进行了扩展. 1.C++定义的结构名.联合名.枚举名 都是 类型名,可以直接用于变量的声明或定义.即在C++中定义变量时不必在结构名.联合名.枚举名 前 ...
- Ms SQLServer中的Union和Union All的使用方法和区别
Ms SQLServer中的Union和Union All的使用方法和区别 SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 ...
- union与union all 的区别
Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...
- Union 与 Union all 区别
原创,请园长不要删 Sql查询统计时,很多时候用到了union 和 union all,union与union all的区别就是联合查询的时候union会去重,union all不会去重.本人用uni ...
- SQL Server函数---Union与Union All的区别
SQL Server函数---Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称 ...
- Oracle中Union与Union All的区别(适用多个数据库)
Oracle中Union与Union All的区别(适用多个数据库) 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或 ...
随机推荐
- 深入理解LInux内核-进程通信
进程间通信的基本机制:1.管道和FIFO(命名管道):最适合在进程之间实现生产者/消费者的交互.进程A向管道写入数据,进程B从管道读出数据.2.信号量:内核信号量的用户态版本.3.消息:允许进程在预定 ...
- IE报错:The given path's format is not supported
在使用FileUpload控件进行上传EXCEL文件时,本地调试上传无问题,但是发布之后报地址无效错误 一.出现这个错误的主要原因是,在本地上传图片的时候HttpPostedFileBase对象里面保 ...
- Custom Sublime Text Build Systems For Popular Tools And Languages
Sublime Text is currently the text editor of choice for a number of developers in the open-source co ...
- 关于CentOS 6下Hadoop占用系统态CPU高的处理办法【转】
一次不经意发现Hadoop的系统态CPU使用率很高,然后百度一下居然是个已知问题. RHEL6优化了内存申请的效率,而且在某些场景下对KVM的性能有明显提升:http://www.Linux-kvm. ...
- FastJson的常用操作
FastJson的常用操作 2017-06-05 常用操作包括以下内容: 对象与(JsonObject或JsonArray)与String的互换 String转换为(JsonObject或JsonAr ...
- Context.startActivity出现AndroidRuntimeException
转:http://hi.baidu.com/huaxinchang/item/e1a771cf4d424312b77a2416 昨天做了一个Activity的启动动画,效果是点击桌面图标先出现动画后启 ...
- JAVA-JSP内置对象之application对象获得服务器版本
相关资料:<21天学通Java Web开发> application对象获得服务器版本1.通过application对象的getMajorVersion()方法和getMinorVersi ...
- 【转】oracle中的游标的原理和使用详解
游标 游标的简介: 逐行处理查询结果,以编程的方式访问数据 游标的类型: 1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐式游标,名字固定叫sql. 2,显式游标:显式游标用 ...
- thinkphp前台模版字符串截取
ThinkPHP\Common\extend.php 中管理前台模版的截取{$vons.title|msubstr=0,26} 原始的代码是无法使用截取支持…. 由于涉及到只有汉字检测最为准确 需要加 ...
- Selenium (4) —— Selenium是什么? WebDriver是什么?做什么?(101 Tutorial)
Selenium (4) -- Selenium是什么? WebDriver是什么?做什么?(101 Tutorial) selenium版本: v2.48.0 (Standalone Seleniu ...