转自: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. thinkphp中的配置与读取C方法详解

    1.项目公共配置 Conf/config.php 内容如下 <?php /** *项目公共配置 *@package *@author **/ return array( 'LOAD_EXT_CO ...

  2. Easyui combotree 获取自定义ID属性方法

    1.设置属性 <input id="cc" class="easyui-combotree" data-options="url:'tree_d ...

  3. USB驱动程序之USB设备驱动程序2鼠标用作键盘学习笔记

    1.usbmouse.c (1)probe函数 在这个probe函数后判断是不是一个鼠标,先得到usb_host_interface结构体,除了端点0外,端点个数如果不是1,返回错误,表示不是自己能支 ...

  4. java mybatisGenerator with velocity

    mybatisGenerator + velocity 模板生成dao+ mapper,并将mysql命名规范的table name + column -> java命名规范的 Class na ...

  5. 【BZOJ】1441 Min(数学)

    题目 传送门:QWQ 分析 裴蜀定理. 因为存在 $ a_1 $ $ a_2 $...... $ a_n $的最大公约数为 $ d $,那么必定存在 $ x_1*a_1+x_2*a_2+...x_n* ...

  6. 强大的NCBI接口

    刚才小玩了下,不错,.net确实很方便,很强大 Using Entrez Utilities Web Service with C# and MS Visual Studio 2005 Updated ...

  7. Julia - 复合表达式

    复合表达式是用一个表达式按照顺序对一系列子表达式求值,并返回最后一个子表达式的值 有两种方法:begin 块和 “;” 链 begin 块 begin 块的多行写法 julia> a = beg ...

  8. pipenv 简要指南

    pipenv 简要指南 pipenv是requests作者的一个项目, 整合了virtualenv, pip, pipfile, 用于更方便地为项目建立虚拟环境并管理虚拟环境中的第三方模块. 安装 直 ...

  9. javaWeb 开发的成长路线

    感觉最近技术到了一个瓶颈,好长时间没有感觉进步了,本着活到老学到老的态度,笔者就去逛了下,看看前辈们写的文章,觉得他们写的非常不错,在这里特别贴出一张特别值得收藏的图片,不是说其他总结的图片不值得收藏 ...

  10. 【转】正则表达式简介及在C++11中的简单使用教程

    正则表达式Regex(regular expression)是一种强大的描述字符序列的工具.在许多语言中都存在着正则表达式,C++11中也将正则表达式纳入了新标准的一部分,不仅如此,它还支持了6种不同 ...