如果我们有一个表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的差别的更多相关文章

  1. mysql union和union all 的差别以及使用

    Union由于要进行反复值扫描,所以效率低.假设合并没有刻意要删除反复行,那么就使用Union All  两个要联合的SQL语句 字段个数必须一样.并且字段类型要"相容"(一致). ...

  2. union和union all用法

    工作中,遇到同事之前写的oracle语句中有一个union all,并且很多地方都用到了.便在网上查了一下用法,以下是自己的理解. union  (联合)将两个或者多个结果集合并. 在使用时,两个结果 ...

  3. mysql中union与union all的区别

    当查询表结构完全相同的多张表的数据时: 1.当查询条件完全相同且不包括主键,此时用union查询会过滤掉查询出的重复的记录,及漏查记录:使用union all进行查询,则会查出所有的符合条件的记录,保 ...

  4. 【转载】SQL语句中Union和Union All的区别

    在使用到SQL语句进行数据库查询的过程中,如果需要求两个数据集合的并集,一般会使用到联合查询关键字Union或者Union All,其实Union和Union All两者的使用有一定差别,查出来的数据 ...

  5. 【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 ...

  6. Oracle 中 union 和union all 的简单使用说明

    1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...

  7. 【转】Mysql联合查询union和union all的使用介绍

    Mysql的联合查询命令UNION和UNION ALL,总结了使用语法和注意事项,以及学习例子和项目例子,需要的朋友可以参考下 一.UNION和UNION ALL的作用和语法 UNION 用于合... ...

  8. SQL UNION 和 UNION ALL 操作符\SQL SELECT INTO 语句\SQL CREATE DATABASE 语句

    SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...

  9. Union和Union All到底有什么区别

    以前一直不知道Union和Union All到底有什么区别,今天来好好的研究一下,网上查到的结果是下面这个样子,可是还是不是很理解,下面将自己亲自验证: Union:对两个结果集进行并集操作,不包括重 ...

随机推荐

  1. oracle死锁模拟

    环境介绍: 用户test01 创建表tab01,用户test02创建表tab02.Test01 更新tab01不提交,test02 更新表tab02不提交.然后test01 更新test02下的表ta ...

  2. [转]C++基本功和 Design Pattern系列 ctor & dtor

    今天Aear讲的是class.ctor 也就是constructor, 和  class.dtor, destructor. 相信大家都知道constructor 和 destructor是做什么用的 ...

  3. [LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

    //定义二维平面上的点struct Point { int x; int y; Point(, ):x(a),y(b){} }; bool operator==(const Point& le ...

  4. python正则表达式入门

    基本概念 使用正则表达式需要import re 表达式前加r避免转义 \d代表数字,\s代表空白字符,\w代表字母+数字. .代表任意单个字符 {m,n}代表前面字符至少出现m次,最多出现n次. (x ...

  5. TalkingData游戏版本在Cocos2d-x 3.0使用

    Cocos2dx在3.0的版本中改动确实不少啊,所以导致原来可以在Cocos2.x版本上的demo都不能直接用,所以不得不重要写一个新的demo 但是TalkingData的库一直都是可以用的,只是之 ...

  6. [UML]UML中几种类间关系

    UML中类间关系主要有六种,分别是继承.实现.依赖.关联.聚合.组合 1.继承 继承是指A类继承B类,继承它private除外的所有属性和方法,这种关系是最常见的关系,在java中使用extends表 ...

  7. dict两种遍历方法

    采用for...in...遍历: >>> for i in dd: ... print("%s:%s"%(i,dd[i])) ... :chen :hang :w ...

  8. Yii2——MYSQL操作

    先创建连接对象 $connection = new \yii\db\Connection([ 'dsn' => $dsn, 'username' => $username, 'passwo ...

  9. UI基础 - UITabBarController

    self.window = [[UIWindow alloc] init]; self.window.frame = [UIScreen mainScreen].bounds; oneViewCont ...

  10. Instruments 使用指南

    Instruments 用户指南 http://cdn.cocimg.com/bbs/attachment/Fid_6/6_24457_90eabb4ed5b3863.pdf 原著:Apple Inc ...