转自:http://www.2cto.com/database/201208/148795.html

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

Union All:对两个结果集进行并集操作,包括重复行,不进行排序;

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

Minus:对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。

  www.2cto.com

具体讲讲Union和Union All。先来看一个例子:

有一张学生表student如下:

id name score

1 Aaron 78

2 Bill 76

3 Cindy 89

4 Damon 90

5 Ella 73

6 Frado 61

7 Gill 99

8 Hellen 56

9 Ivan 93

10 Jay 90

看看以下4段SQL语句

[sql]

1)

select * from student where id<4

union

select * from student where 2<id and id<6

2)

select * from student where 2<id and id<6

union

select * from student where id<4

    www.2cto.com

3)

select * from student where id<4

union all

select * from student where 2<id and id<6

4)

select * from student where 2<id and id<6

union all

select * from student where id<4

看看4段SQL语句的执行结果:

id name score

1 Aaron 78

2 Bill 76

3 Cindy 89

4 Damon 90

5 Ella 73

同上

  www.2cto.com

id name score

1 Aaron 78

2 Bill 76

3 Cindy 89

3 Cindy 89

4 Damon 90

5 Ella 73

id name score

3 Cindy 89

4 Damon 90

5 Ella 73

1 Aaron 78

2 Bill 76

3 Cindy 89

从以上四个结果来看,可以得出结论:Union是不可重复的,有序的。Union All是可以重复的,无序的。

那么Union的自动排序的默认规则是什么呢?

众多周知,以上的select *就相当于select id, name, score。默认排序规则是按照select之后的第一个字段排序,也就是id。如果想要按照score排序,可以这样写:select score, id, name。

那么可不可以用order by排序呢,答案是:可以。不过order by一定要写到最后一个结果集里,如下面SQL语句:

[sql]

select * from student where 2<id and id<6

union    www.2cto.com

select * from student where id<4

order by score

order by 排序对于Union、Union All、Intersect、Minus都有效。

Union、Union All、Intersect、Minus的更多相关文章

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

  2. oracle之集合操作函数---minus、union、intersect

    集合操作符专门用于合并多条select语句的结果,包括:UNION,UNION ALL,INTERSECT,MINUS.当使用集合操作函数时,需保证数据集的字段数据类型和数目一致. 使用集合操作符需要 ...

  3. 【转】Oracle集合操作函数:union、intersect、minus

    集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配. 集合操作符 ...

  4. Oracle集合操作函数:union、intersect、minus

    [转]Oracle集合操作函数:union.intersect.minus 集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINU ...

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

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

  6. Union、Union All、Intersect、Minus用法和区别

    假设我们有一个表Student,包括以下字段与数据: [c-sharp] view plain copydrop table student;    create table student  (   ...

  7. SQL中intersect、union、minus和except 运算符

    1.intersect运算符intersect运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表.当 ALL 随 INTERSECT 一起使用时 (inte ...

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

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

  9. Linq to SQL -- Union All、Union、Intersect和Top、Bottom和Paging和SqlMethods

    Union All/Union/Intersect操作 适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1. ...

随机推荐

  1. python复习之路-Day01

    数据类型初识 1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4.(-5+4j)和 ...

  2. Codeforces-417D总结&题解

    在分析题目前,一定要完全读懂题目意思,否则一些都是白搭.这道理谁都懂,但是能每次都做到的人不多.比如本人,这次又粗心地在这里疯狂地踩坑了. (1)本题有这么一句话:But Gena's friends ...

  3. Metasploit对安卓手机的攻击

    首先要kali进行内网穿透,可参考http://www.cnblogs.com/sch01ar/p/7562954.html 首先生成一个apk木马 命令:msfvenom -p android/me ...

  4. [POJ] Bode Plot

    Description Consider the AC circuit below. We will assume that the circuit is in steady-state. Thus, ...

  5. 02_java之数据类型和基本运算

    01变量概述 * A: 什么是变量? * a: 变量是一个内存中的小盒子(小容器),容器是什么?生活中也有很多容器,例如水杯是容器,用来装载水:你家里的大衣柜是容器,用来装载衣裤:饭盒是容器,用来装载 ...

  6. angularjs之级联菜单

    原理: 1.ng-options中val.id as val.name for val in cascading 将id的值赋给 mg-modelone2.在通过ng-change传给函数3.当一级下 ...

  7. DataMatrix二维条码源码分析检测识别图像位置

    发布时间:2014-10-31 DataMatrix的代码结构和QR码基本相同: 其中Detector的功能还是从原始图像中找出符号码的部分,并且进行透视转换纠正扭曲. 其解码流程与QR码差不多,关键 ...

  8. MyBatis初识(通过小实例清晰认识MyBatis)

    1.MyBatis框架: MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...

  9. 关于PHP如何用实现防止用户在浏览器上使用后退功能重复提交输入

    $(function(){ if(window.history && window.history.pushState){ $(window).on('popstate',functi ...

  10. 给用户root权限

    1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加一个名为tommy的用户#passwd tommy   #passwd tommy //修改密 ...