来源:http://blog.csdn.net/wanghai__/article/details/4712555/

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

  1. drop table student;
  2. create table student
  3. (
  4. id int primary key,
  5. name nvarchar2(50) not null,
  6. score number not null
  7. );
  8. insert into student values(1,'Aaron',78);
  9. insert into student values(2,'Bill',76);
  10. insert into student values(3,'Cindy',89);
  11. insert into student values(4,'Damon',90);
  12. insert into student values(5,'Ella',73);
  13. insert into student values(6,'Frado',61);
  14. insert into student values(7,'Gill',99);
  15. insert into student values(8,'Hellen',56);
  16. insert into student values(9,'Ivan',93);
  17. insert into student values(10,'Jay',90);
  18. commit;

首先,我们来看一下UNION的例子:

  1. SQL> select *
  2. 2  from student
  3. 3  where id<4
  4. 4  union
  5. 5  select *
  6. 6  from student
  7. 7  where id>2 and id<6
  8. 8  ;
  9. ID NAME                                SCORE
  10. ---------- ------------------------------ ----------
  11. 1 Aaron                                  78
  12. 2 Bill                                   76
  13. 3 Cindy                                  89
  14. 4 Damon                                  90
  15. 5 Ella                                   73
  16. SQL>

如果换成Union All连接两个结果集,则结果如下:

  1. SQL> select *
  2. 2  from student
  3. 3  where id<4
  4. 4  union all
  5. 5  select *
  6. 6  from student
  7. 7  where id>2 and id<6
  8. 8  ;
  9. ID NAME                                SCORE
  10. ---------- ------------------------------ ----------
  11. 1 Aaron                                  78
  12. 2 Bill                                   76
  13. 3 Cindy                                  89
  14. 3 Cindy                                  89
  15. 4 Damon                                  90
  16. 5 Ella                                   73
  17. 6 rows selected.

可以看到,Union和Union All的区别之一在于对重复结果的处理。

接下来,我们交换一个两个SELECT语句的顺序,看看结果是怎样的。

  1. SQL> select *
  2. 2  from student
  3. 3  where id>2 and id<6
  4. 4  union
  5. 5  select *
  6. 6  from student
  7. 7  where id<4
  8. 8  ;
  9. ID NAME                                SCORE
  10. ---------- ------------------------------ ----------
  11. 1 Aaron                                  78
  12. 2 Bill                                   76
  13. 3 Cindy                                  89
  14. 4 Damon                                  90
  15. 5 Ella                                   73
  16. SQL> select *
  17. 2  from student
  18. 3  where id>2 and id<6
  19. 4  union all
  20. 5  select *
  21. 6  from student
  22. 7  where id<4
  23. 8  ;
  24. ID NAME                                SCORE
  25. ---------- ------------------------------ ----------
  26. 3 Cindy                                  89
  27. 4 Damon                                  90
  28. 5 Ella                                   73
  29. 1 Aaron                                  78
  30. 2 Bill                                   76
  31. 3 Cindy                                  89
  32. 6 rows selected.

可以看到,对于UNION来说,交换两个SELECT语句的顺序后结果仍然是一样的,这是因为UNION会自动排序。而UNION ALL在交换了SELECT语句的顺序后结果则不相同,因为UNION ALL不会对结果自动进行排序。

那么这个自动排序的规则是什么呢?我们交换一下SELECT后面选择字段的顺序(前面使用SELECT *相当于SELECT ID,NAME,SCORE),看看结果如何:

  1. SQL> select score,id,name
  2. 2  from student
  3. 3  where id<4
  4. 4  union
  5. 5  select score,id,name
  6. 6  from student
  7. 7  where id>2 and id<6
  8. 8  ;
  9. SCORE         ID NAME
  10. ---------- ---------- ------------------------------
  11. 73          5 Ella
  12. 76          2 Bill
  13. 78          1 Aaron
  14. 89          3 Cindy
  15. 90          4 Damon

可是看到,此时是按照字段SCORE来对结果进行排序的(前面SELECT *的时候是按照ID进行排序的)。

那么有人会问,如果我想自行控制排序,能不能使用ORDER BY呢?当然可以。不过在写法上有需要注意的地方:

  1. select score,id,name
  2. from student
  3. where id > 2 and id < 7
  4. union
  5. select score,id,name
  6. from student
  7. where id < 4
  8. union
  9. select score,id,name
  10. from student
  11. where id > 8
  12. 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. Union和Union All到底有什么区别

    以前一直不知道Union和Union All到底有什么区别,今天来好好的研究一下,网上查到的结果是下面这个样子,可是还是不是很理解,下面将自己亲自验证: Union:对两个结果集进行并集操作,不包括重 ...

  2. union和union all的区别

    UNION 写一篇联合查询(把前后两个表的查询结果集合在前表中)首先有个为什么需要 相同记录数?? 记错了.应该是union两张表的查询字段数目要一致,字段类型要相似相同的数据类型,至少是相似,可转化 ...

  3. Oracle之Union与Union all的区别

    如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并在一起显示出来. union和unio ...

  4. union与union all的区别

    首先说下union与join的区别 1.union是以行增加的方式,进行连接:join是以列增加的方式进行连接: 2.union连接查询的两个表的字段必须要一一对应,数目相等:join则没有要求,但是 ...

  5. C和C++中结构体(struct)、联合体(union)、枚举(enum)的区别

    C++对C语言的结构.联合.枚举 这3种数据类型进行了扩展. 1.C++定义的结构名.联合名.枚举名 都是 类型名,可以直接用于变量的声明或定义.即在C++中定义变量时不必在结构名.联合名.枚举名 前 ...

  6. Ms SQLServer中的Union和Union All的使用方法和区别

    Ms SQLServer中的Union和Union All的使用方法和区别 SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 ...

  7. union与union all 的区别

    Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...

  8. Union 与 Union all 区别

    原创,请园长不要删 Sql查询统计时,很多时候用到了union 和 union all,union与union all的区别就是联合查询的时候union会去重,union all不会去重.本人用uni ...

  9. SQL Server函数​---Union与Union All的区别

    SQL Server函数---Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称 ...

  10. Oracle中Union与Union All的区别(适用多个数据库)

    Oracle中Union与Union All的区别(适用多个数据库) 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或 ...

随机推荐

  1. 深入理解LInux内核-进程通信

    进程间通信的基本机制:1.管道和FIFO(命名管道):最适合在进程之间实现生产者/消费者的交互.进程A向管道写入数据,进程B从管道读出数据.2.信号量:内核信号量的用户态版本.3.消息:允许进程在预定 ...

  2. IE报错:The given path's format is not supported

    在使用FileUpload控件进行上传EXCEL文件时,本地调试上传无问题,但是发布之后报地址无效错误 一.出现这个错误的主要原因是,在本地上传图片的时候HttpPostedFileBase对象里面保 ...

  3. Custom Sublime Text Build Systems For Popular Tools And Languages

    Sublime Text is currently the text editor of choice for a number of developers in the open-source co ...

  4. 关于CentOS 6下Hadoop占用系统态CPU高的处理办法【转】

    一次不经意发现Hadoop的系统态CPU使用率很高,然后百度一下居然是个已知问题. RHEL6优化了内存申请的效率,而且在某些场景下对KVM的性能有明显提升:http://www.Linux-kvm. ...

  5. FastJson的常用操作

    FastJson的常用操作 2017-06-05 常用操作包括以下内容: 对象与(JsonObject或JsonArray)与String的互换 String转换为(JsonObject或JsonAr ...

  6. Context.startActivity出现AndroidRuntimeException

    转:http://hi.baidu.com/huaxinchang/item/e1a771cf4d424312b77a2416 昨天做了一个Activity的启动动画,效果是点击桌面图标先出现动画后启 ...

  7. JAVA-JSP内置对象之application对象获得服务器版本

    相关资料:<21天学通Java Web开发> application对象获得服务器版本1.通过application对象的getMajorVersion()方法和getMinorVersi ...

  8. 【转】oracle中的游标的原理和使用详解

    游标 游标的简介: 逐行处理查询结果,以编程的方式访问数据 游标的类型: 1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐式游标,名字固定叫sql. 2,显式游标:显式游标用 ...

  9. thinkphp前台模版字符串截取

    ThinkPHP\Common\extend.php 中管理前台模版的截取{$vons.title|msubstr=0,26} 原始的代码是无法使用截取支持…. 由于涉及到只有汉字检测最为准确 需要加 ...

  10. Selenium (4) —— Selenium是什么? WebDriver是什么?做什么?(101 Tutorial)

    Selenium (4) -- Selenium是什么? WebDriver是什么?做什么?(101 Tutorial) selenium版本: v2.48.0 (Standalone Seleniu ...