union 连接两个表查询的结果

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

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

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

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

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

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

[c-sharp] view plaincopy
  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子句改变排序方式。

转载于:https://www.cnblogs.com/chyg/archive/2012/11/24/2785424.html

sql中union和union all的区别相关推荐

  1. SQL中char varchar nchar nvarchar ntext区别和使用(资料汇总)

    SQL中char varchar nchar nvarchar ntext区别和使用(资料汇总) 2008年10月14日 星期二 23:57 一.SQL中char varchar nchar nvar ...

  2. SQL中的or与in的区别

    SQL中的or与in的区别 or与in一样的案例(条件里只有a时) a in (1,2,3) a = 1 or a = 2 or a = 3 or与in不一样的案例(条件里不止a时) where a ...

  3. SQL中JOIN和UNION区别及用法

    转载:http://chengheng1984.blog.163.com/blog/static/17947412201012215738844/ JOIN用于按照ON条件联接两个表,主要有四种: I ...

  4. SQL中的cast 和convert的区别,日期操作

    SQL Server 日期格式和日期操作 SQL Server发展至今, 1.关于日期的格式的控制方法, 有传统的方法,比如CONVERT(), 也有比较便利的新方法,比如FORMAT(): eg: ...

  5. Sql中的并(UNION)、交(INTERSECT)、差(minus)、除去(EXCEPT)详解

    限制条件 (1)所有查询中的列数和列的顺序必须相同. (2)比较的两个查询结果集中的列数据类型可以不同但必须兼容. (3)比较的两个查询结果集中不能包含不可比较的数据类型(xml.text.ntext ...

  6. SQL中truncate table和delete的区别

    truncate table table_namedelete from table_namedrop table table_name truncate table在功能上与不带where子句的de ...

  7. sql中in和exist语句的区别?

    两者都能实现表功能查询,主要区别如下: 1.适用表的类型不同. in是子查询为驱动表,外面的表为被驱动表,故适用于子查询结果集小而外面的表结果集大的情况. exists是外面的表为驱动表,子查询里面的 ...

  8. SQL中NVL和NVL2有什么区别,以及NULLIF 的使用

    1.NVL 格式:NVL (expr1, expr2) 含义:expr1为NULL,返回expr2:不为NULL,返回expr1. 注意两者的类型要一致 2.NVL2 格式:NVL2 (expr1, ...

  9. SQL中varchar和nvarchar有什么区别?

    varchar(n) 长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储大小为输入数据的字节的实际长度,而不是 n 个字节. nva ...

  10. SQL中truncate table和delete的区别 --转

    内容: http://www.cnblogs.com/GT_Andy/archive/2010/01/28/1921871.html 感谢博主的分享!!! 转载于:https://www.cnblog ...

最新文章

  1. Prometheus监控业务指标
  2. 您不是订单管理的定向开发者_Web Summit 2020大会:华为在欧洲发布HMS Connect,持续助力合作伙伴与开发者的创新增长...
  3. php fopen 错误,php fopen函数失败怎么办
  4. 51nod 1096 距离之和最小 思维题,求中位数
  5. Eclipse常用的快捷方式
  6. jQuery-1.9.1源码分析系列(五) 回调对象
  7. echo怎么把日志清空_shell脚本清空系统message日志
  8. c语言自动变量与静态变量,C语言的中的静态变量和局部变量(自动变量)
  9. Foxconn Core Concept
  10. Raphael的set使用
  11. Matlab Tricks(十)—— padarray 的实现
  12. 【搜索引擎】强推!最好用资源最全的十个百度网盘搜索引擎
  13. 页面中文乱码,tomcat服务器,jsp乱码
  14. WiFi的信道与关联
  15. Android天天飞车游戏辅助系统
  16. Linguistic Regularities in Continuous Space Word Representations
  17. 写paper之ppt画图——不定期更新
  18. 阿里云云效GitCode迁移至GitLab仓库
  19. Win7和Win10如何使文件的视图默认按详细信息显示
  20. 让 AE 输出 MPEG

热门文章

  1. SharePoint 使用代码创建 SPWeb/SPSiite/SPWebApplication以及WebPart添加到页面与删除 (一)...
  2. Ubuntu14.04 桌面 launcher 终端 状态栏 失效解决
  3. 计划继续深化学习物联网系统的相关知识 目前的WiFi模块基于ESP8266
  4. 【教女朋友学网络系列3】之手把手教她明白交换机的基本原理
  5. oracle ajax储存过程分页,创建 Oracle 分页存储过程
  6. mybatis与mysql的优点_MyBatis的优缺点以及特点
  7. rap技术原理_「水深坑多」做分子海绵,你还需要了解这些技术
  8. python 用户输入_Python中如何让用户输入内容
  9. 问题 D: AC自动机(二分,第一个等于和最后一个等于)
  10. 阿里云视图计算,边缘计算的主“战”场