假设我们有一个表Student,包括以下字段与数据:

[c-sharp] view plain copy
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的例子:

[c-sharp] view plain copy
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连接两个结果集,则结果如下:

[c-sharp] view plain copy
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语句的顺序,看看结果是怎样的。

[c-sharp] view plain copy
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),看看结果如何:

[c-sharp] view plain copy
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呢?当然可以。不过在写法上有需要注意的地方:

[c-sharp] view plain copy
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子句改变排序方式。
---------------------
作者:wh62592855
来源:CSDN
原文:https://blog.csdn.net/wanghai__/article/details/4712555
版权声明:本文为博主原创文章,转载请附上博文链接!

Union、Union All、Intersect、Minus用法和区别的更多相关文章

  1. Oracle中的Union、Union All、Intersect、Minus

    Oracle中的Union.Union All.Intersect.Minus  众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括 ...

  2. ORACLE中union/union all/Intersect/Minus用法

    Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All,对两个结果集进行并集操作,包括重复行,不进行排序: Intersect,对两个结果集进行交集操作,不包 ...

  3. Oracle中的Union、Union All、Intersect、Minus[转]

    众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括以下字段与数据: drop table student; create table ...

  4. Oracle SQL Lesson (8) - 使用集合操作符(Union,Intersect,Minus)

    集合操作符UNION/UNION ALLINTERSECTMINUS Union All不排序,不去重,其余均升序且去重.create table e1 as select * from emp wh ...

  5. Union、Union All、Intersect、Minus

    转自:http://www.2cto.com/database/201208/148795.html Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All: ...

  6. 集合操作符 Union / Union All / Intersect / Minus

    集合操作符 Union / UnionAll / Intersect / Minus -- 生成测试数据 create table dept_01 as select * from dept wher ...

  7. sql inner join , left join, right join , union,union all 的用法和区别

    Persons 表: Id_P LastName FirstName Address City 1 Adams John Oxford Street London 2 Bush George Fift ...

  8. 数据库学习之五--Union, Union All和Intersect

    一.定义 Union操作符用于合并两个或多个SELECT语句的结果集: 注:1. Union连接的Select语句之间必须拥有相同数量的列: 2. 列也必须拥有相似的数据类型: 3. 每条 SELEC ...

  9. set和enum类型的用法和区别

    mysql中的set和enum类型的用法和区别 mysql中的enum和set其实都是string类型的而且只能在指定的集合里取值, 不同的是set可以取多个值,enum只能取一个值.   1 2 3 ...

随机推荐

  1. express——crud

    使用express框架做一个简单的增删改查demo,先上效果图: 1.使用webstrom新建一个express项目,建好的项目文件是这样的: 2.直接上代码,方便学习db.js /** * Crea ...

  2. 【CSS系列】布局篇

    一.让设计居中 1.使用自动空白边让设计居中 <style type="text/css"> body{ text-align:center; min-width:76 ...

  3. 常用的vue辅助工具vue-devtools

    1,下载: https://github.com/datura-lj/vuedevtools 2,将下载好的文件拖到chrome拓展栏中(更多工具=>拓展程序): 3,修改计算机配置文件: wi ...

  4. 【BZOJ4282】慎二的随机数列 乱搞

    [BZOJ4282]慎二的随机数列 Description 间桐慎二是间桐家著名的废柴,有一天,他在学校随机了一组随机数列, 准备使用他那强大的人工智能求出其最长上升子序列,但是天有不测风云,人有旦夕 ...

  5. 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分

    [BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...

  6. angularJs初体验,实现双向数据绑定!使用体会:比较爽

    使用初体验:ng 双向数据绑定: 最简单的双向数据绑定:(使用默认模块控制) <body ng-app> <input type="text" ng-model= ...

  7. pycharm 和 Anaconda 下的 opencv 安装

    学习真的切忌三天打鱼两天晒网!! 一开始python下的opencv已经都弄好了,中间电脑坏了一次,好久没有接触这个,就全部都忘完了.深感惋惜. 今天又从新安装了一下opencv.在anaconda下 ...

  8. python 10分钟入门pandas

    本文是对pandas官方网站上<10 Minutes to pandas>的一个简单的翻译,原文在这里.这篇文章是对pandas的一个简单的介绍,详细的介绍请参考:Cookbook .习惯 ...

  9. DetaSet更新数据

    用到的控件:DataGridView(展示数据),                    Button控件,更名[更新] using System; using System.Collections. ...

  10. LOL TGP更新影响VS debug 问题

    刚才看群里说到VS无法调试,出现"无法使用xxx附加到应用程序'webdev.webserver...'"的问题,群友提出自己的经历,可能是LOL TGP的问题. 提问者卸载了TG ...