如果我们有一个表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. ExcelApplication 另存Excel的SaveAs函数

    procedure SaveAs(const Filename: WideString; FileFormat: OleVariant; Password: OleVariant; WriteResP ...

  2. android studio主题设置-笔记3

    主题背景设置(就是工具黑色背景还是白色背景),路径:File-Settings-Appearance

  3. C# static 干货全解析

    讲解顺序 背景 静态字段 静态函数 静态方法 疑问解答 背景 static来源 在编写类的时候,有时候需要类里面的某个成员具有唯一性,也就是,对所有的对象都保持只有一个的状态.比如创建个人信息,我们都 ...

  4. HashMap、HashTable学习

    HashMap: HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io.Serializable接口. HashMap 的实现不是同步的,这意味着它不是线程安 ...

  5. C++在使用Qt中SLOT宏需要注意的一个小细节

    大家都知道C++虚函数的机制,对于基类定义为虚函数的地方,子类如果覆写,在基类指针或者引用来指向子类的时候会实现动态绑定. 但如果指针去调用非虚函数,这个时候会调用C++的静态绑定,去判断当前的指针是 ...

  6. webservice取文件修改时间,返回1601/1/1 8:00:00

    若文件查找不到,则会返回1601/1/1 8:00:00,若能正确查找到该文件,则返回正确的修改时间.

  7. 字符串处理——strpos()函数

    strpos() 函数返回字符串在另一个字符串中第一次出现的位置. 大小写敏感 如果没有找到该字符串,则返回 false. strpos(string,find,start)  string 必需:规 ...

  8. phalcon安装和输出 hello word

    1:下载和安装Wampserver2.4-x86.exe 服务器: 2:到phalcon官方网站下载对应的dll文件 phalcon_x86_VC9_php5.4.0_1.2.5 我下的是这个版本 所 ...

  9. The '_imaging' module for the PIL could not be imported: DLL load failed: The specified module could not be found

    I uninstalled the PIL and installed the Pillow and the problem solved.PIL worked fine for me with th ...

  10. bzoj2067: [Poi2004]SZN

    Description String-Toys joint-stock 公司需要你帮他们解决一个问题. 他们想制造一个没有环的连通图模型. 每个图都是由一些顶点和特定数量的边构成. 每个顶点都可以连向 ...