Union和Union All的差别
如果我们有一个表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子句改变排序方式。
最新文章
- 编译器 cc、gcc、g++、CC 的区别
- 关于在left join的on子句中限制左边表的取值时出现非期望的结果
- java1234教程系列笔记 S1 Java SE chapter 02 lesson 03 java基本数据类型
- PHP下的命令行执行 php -S localhost -t public
- zabbix告警“Zabbix poller processes more than 75% busy”
- 如何做好IT运营.
- CodeForces Round 199 Div2
- Problem 1007 幸运数 线段树成段更新
- IIS 7.0、IIS 7.5 和 IIS 8.0 中的 HTTP 状态代码 转
- 使用AVCaptureSession捕捉静态图片
- SQL中使用的一些函数问题
- old linkedin profile
- HTML5基本标签的使用
- stm32通用定时器详解
- 安装pycrypto2.6.1报错
- Java 容器源码分析之Map-Set-List
- MPLS笔记
- 构建Redis主从镜像
- SD从零开始57-58,第三方订单处理,跨公司销售
- 绝对良心提供百度网盘的jdk1.8源码下载包含sun包的
热门文章
- 关于java中Double类型的运算精度问题(转)
- c - static 变量
- NSString截取字符串
- JavaScript--循环--打印星星和99乘法表
- 你好,C++(39)6.4.4 依葫芦画瓢:用C++表达设计结果(下)
- hdu 1281棋盘游戏(二分匹配)
- ORA-00933 UNION 与 ORDER BY
- asp.net Server.HtmlEncode和HtmlDecode
- 利用WebApi获取手机号码归属地
- TCP回射客户程序:main函数