转自: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. Apache common exec包的相应使用总结

      最近在写一个Java工具,其中调用了各种SHELL命令,使用了Runtime.getRuntime().exec(command);这个方法.但是在我调用某个命令执行操作时,程序就卡在那了,但是其 ...

  2. JAVA构造函数在超类和子类调用注意事项

    1.构造函数:        当子类继承一个父类时,构造子类时需要调用父类的构造函数,存在三种情况        (1),父类无构造函数或者一个无参数构造函数,子类若无构造函数或者有无参数构造函数,子 ...

  3. laravel-ide-helper 遇到There are no commands defined问题怎么解决

    laravel门面类的代码提示方案: https://github.com/barryvdh/laravel-ide-helper 按照步骤安装 1.composer require barryvdh ...

  4. Rhythmk 一步一步学 JAVA(7): jsp 自定义标签

    1.实现Tag接口: TagSupport类实现了Tag接口,为我们提供了4个重要的方法(见表6-5). 1.1. TagSupport类中的常用方法           int doStartTag ...

  5. 运维自动化工具 Cobbler

    简介: 关于操作系统安装方面的自动化,早前我们使用 RedHat 推出的 Kickstart 来批量安装操作系统,近年来 RedHat 又推出一个 Cobbler . Cobbler 使用 Pytho ...

  6. 设置Windows开机自动启动VirtualBox虚拟机系统

    如果常用VirtualBox虚拟机系统的话,设置随开机启动也是很方便的.不需要打开VirtualBox窗口,直接启动VirtualBox虚拟机系统就可以了. 设置开机自启动VirtualBox虚拟机系 ...

  7. slf4j日志框架

  8. 关于js闭包的一些浅层面的理解

    function a() { var i = 0; function b() { alert(++i); } return b; } var c = a(); c(); 1.js分全局作用域和函数作用 ...

  9. C#登陆界面学习编写 2018.08.03

    简单的登陆界面的编写,在编写如下界面时,设置错误次数上限需要用到静态变量 public static int count;//计算错误次数(为静态变量) 可以防止点击登陆后次数被清空,在登陆后打开新的 ...

  10. java算法 蓝桥杯算法训练 Fibonacci数列

    问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...