Union和Union All的区别

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

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

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

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

[c-sharp] view plaincopyprint?
  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>

SQL> select * 2 from student 3 where id<4 4 union 5 select * 6 from student 7 where id>2 and id<6 8 ; ID NAME SCORE ---------- ------------------------------ ---------- 1 Aaron 78 2 Bill 76 3 Cindy 89 4 Damon 90 5 Ella 73 SQL>

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

[c-sharp] view plaincopyprint?
  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.

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

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

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

[c-sharp] view plaincopyprint?
  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.

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

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

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

[c-sharp] view plaincopyprint?
  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

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

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

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

[c-sharp] view plaincopyprint?
  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

select score,id,name from student where id > 2 and id < 7 union select score,id,name from student where id < 4 union select score,id,name from student where id > 8 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. php union all,Union与Union All的区别

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

  2. mysql join union_MySQL中union和join语句使用区别的辨析教程

    union和join是需要联合多张表时常见的关联词,具体概念我就不说了,想知道上网查就行,因为我也记不准确. 先说差别:union对两张表的操作是合并数据条数,等于是纵向的,要求是两张表字段必须是相同 ...

  3. Union和Union All到底有什么区别

    转自:https://www.cnblogs.com/wen-zi/p/9133754.html 以前一直不知道Union和Union All到底有什么区别,今天来好好的研究一下,网上查到的结果是下面 ...

  4. UNION和UNION ALL有什么区别?

    UNION和UNION ALL什么区别? #1楼 您可以通过运行以下查询来避免重复,并且运行速度仍然比UNION DISTINCT(实际上与UNION相同)快得多: SELECT * FROM myt ...

  5. sql中union 和 union all的区别

    最近发现一个视图出奇的慢,在生产环境还好,由于服务器配置较高,没有察觉出来.但是做了一次修改后在开发版 和测试版就直接查询不出结果了.就连select count(1) from 都运行2个小时没有结 ...

  6. Oracle中的Union、Union All、Intersect、Minus 使用用法区别

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

  7. SQL Union 和 Union All 的区别以及二者的性能问题 - 使用Sqlite演示

    1 Union 和 Union All 的区别 Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All:对两个结果集进行并集操作,包括重复行,不进行排序: 也 ...

  8. sql中union和union all的区别

    union 连接两个表查询的结果 假设我们有一个表Student,包括以下字段与数据: [c-sharp] view plaincopy drop table student; create tabl ...

  9. Union与Union All的区别

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

最新文章

  1. C# 类型实例化的语法糖--unity下诡异结果
  2. 彻底理解Toast原理和解决小米MIUI系统上没法弹Toast的问题
  3. expandableListView 总结
  4. 退耦电容,旁路电容和滤波电容的一些区别?
  5. 初二计算机辅导记录,信息技术指导老师的讲课笔记十篇(2)
  6. 服务器不稳定 如何让百度重新收录网站,教你如何让百度重新收录首页
  7. 关于ios 里面碰到内存错误的两种设置
  8. pytorch中获取模型参数
  9. Deploying Exchange 2010(三):在Windows Server 2008上安装Mailbox
  10. 常用图像数据集大全(分类,跟踪,分割,检测等)
  11. redis 过期删除策略和淘汰策略 -redis设计与实现笔记
  12. c语言混响,混响插件( 2cAudio Aether)
  13. python卡方拟合性检验_卡方拟合优度检验
  14. 42表盘直径是从哪测量_表盘直径怎么算,怎样测量手表表盘的直径
  15. mysql 8.0开启远程访问
  16. web前端期末大作业:美食网站设计与实现——HTML+CSS+JavaScript休闲美食餐饮公司网站静态模板(6个页面)
  17. python时间戳转换
  18. 动词17【続】【長】【永】【存】
  19. 权限模型 DAC ACL RBAC ABAC
  20. 智能实验室-专用链转换 1.5.0.150

热门文章

  1. 名词解释_写字楼租赁相关名词解释
  2. suse linux 备份,suse linux利用scp实现自动远程备份
  3. 什么是java序列化_什么是Java序列化?为什么序列化?序列化有哪些方式?
  4. python图形缝隙填充_Python,如何缝合图像哪些重叠区域?
  5. vantui框架_vue移动端优秀框架收集
  6. 【c语言】蓝桥杯算法提高 约数个数
  7. 【c语言】蓝桥杯算法提高 统计平均成绩
  8. mysql 单实例部署_MySQL 5.5单实例 编译安装
  9. cmd指令大全指令_Linux 超全实用指令大全 | CSDN 博文精选
  10. JavaWEB开发04——JQuery