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子句改变排序方式。
Union和Union All的差别的更多相关文章
- mysql union和union all 的差别以及使用
Union由于要进行反复值扫描,所以效率低.假设合并没有刻意要删除反复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样.并且字段类型要"相容"(一致). ...
- union和union all用法
工作中,遇到同事之前写的oracle语句中有一个union all,并且很多地方都用到了.便在网上查了一下用法,以下是自己的理解. union (联合)将两个或者多个结果集合并. 在使用时,两个结果 ...
- mysql中union与union all的区别
当查询表结构完全相同的多张表的数据时: 1.当查询条件完全相同且不包括主键,此时用union查询会过滤掉查询出的重复的记录,及漏查记录:使用union all进行查询,则会查出所有的符合条件的记录,保 ...
- 【转载】SQL语句中Union和Union All的区别
在使用到SQL语句进行数据库查询的过程中,如果需要求两个数据集合的并集,一般会使用到联合查询关键字Union或者Union All,其实Union和Union All两者的使用有一定差别,查出来的数据 ...
- 【oracle】union、union all、intersect、minus 的用法及区别
一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...
- Oracle 中 union 和union all 的简单使用说明
1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...
- 【转】Mysql联合查询union和union all的使用介绍
Mysql的联合查询命令UNION和UNION ALL,总结了使用语法和注意事项,以及学习例子和项目例子,需要的朋友可以参考下 一.UNION和UNION ALL的作用和语法 UNION 用于合... ...
- SQL UNION 和 UNION ALL 操作符\SQL SELECT INTO 语句\SQL CREATE DATABASE 语句
SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...
- Union和Union All到底有什么区别
以前一直不知道Union和Union All到底有什么区别,今天来好好的研究一下,网上查到的结果是下面这个样子,可是还是不是很理解,下面将自己亲自验证: Union:对两个结果集进行并集操作,不包括重 ...
随机推荐
- Linux下安装php加速组件XCache
这里选择的是稳定版本的1.2.2版本,2.0版本的不稳定.wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gzt ...
- drop table xx purge
drop table xx purge; 说明: 所有删除的表都会在回收站里面,只有后面加上purge才是彻底的清空表. (一般用于测试.练习数据表,所以最好不要带purge,要不误删就找不到了.)
- 七、C# 接口
并非只能通过继承使用多态性,还能通过接口使用它. 和抽象类不同,接口不包含任何实现(方法). 然后和抽象类相似,接口也定义了一系列成员,调用者可以依赖这些成员来支持一个特定的功能. 实现接口的类会 ...
- Android入门随记
1.Activity是通过startActivity()开始的,结束后不反回任何结果,而用startActivityForResult(Intent intent, int resquestCode) ...
- JavaScript的“闭包”到底是什么(2)
我的上篇博客标题不对,造成一些误解.我认为博客的宗旨不是背教科书,而是分享研发心得.我的上篇标题因该改成“JavaScript 闭包的一个议题:它对outer scope 的影响”,因为我没有严格地去 ...
- [转自已]Windos多个文件快速重命名说明+图解
转自己以前的文章,给新博客带点气氛. 1.(复制的)比如在文件夹中包含yin.jpg.ye.jpg.zou.jpg三个文件,你希望将它们命名为"photo+数字"的文件名形式,那么 ...
- underscorejs-some学习
2.11 some 2.11.1 语法: _.some(list, predicate, [context]) 2.11.2 说明: 对list集合的每个成员根据predicate进行真值检测,如果一 ...
- Apache Commons DbUtils Problem
- jquery easy ui 学习 (3) window 限制在父类窗体内
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ROS是Robot Operating System
ROS是Robot Operating System 机器人操作系统ROS | 简介篇 同样,从个人微信公众号Nao(ID:qRobotics)搬运. 前言 先放一个ROS Industrial一 ...