---
title: "Union和Union All的区别"
author: "wangyanlong0107"
date: "2012-11-09T15:17:17Z"
tags: []
categories: ["数据库"]
slug: "union-union-all"
draft: false
lastmod: "2012-11-09T15:18:01Z"
id: 1721802
url: "https://bigdragon.iteye.com/blog/1721802"
---

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

 

 

**\[c-sharp]** [view plain](http://blog.csdn.net/wh62592855/article/details/4712555 "view plain")[copy](http://blog.csdn.net/wh62592855/article/details/4712555 "copy")

01. drop table student;
02. create table student
03. (
04. id int primary key,
05. name nvarchar2(50) not null,
06. score number not null
07. );
08. insert into student values(1,'Aaron',78);
09. 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 plain](http://blog.csdn.net/wh62592855/article/details/4712555 "view plain")[copy](http://blog.csdn.net/wh62592855/article/details/4712555 "copy")

01. SQL&gt; select *
02. 2  from student
03. 3  where id&lt;4
04. 4  union
05. 5  select *
06. 6  from student
07. 7  where id&gt;2 and id&lt;6
08. 8  ;
09. 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&gt;

 

 

如果换成Union All连接两个结果集，则结果如下：

 

 

**\[c-sharp]** [view plain](http://blog.csdn.net/wh62592855/article/details/4712555 "view plain")[copy](http://blog.csdn.net/wh62592855/article/details/4712555 "copy")

01. SQL&gt; select *
02. 2  from student
03. 3  where id&lt;4
04. 4  union all
05. 5  select *
06. 6  from student
07. 7  where id&gt;2 and id&lt;6
08. 8  ;
09. 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 plain](http://blog.csdn.net/wh62592855/article/details/4712555 "view plain")[copy](http://blog.csdn.net/wh62592855/article/details/4712555 "copy")

01. SQL&gt; select *
02. 2  from student
03. 3  where id&gt;2 and id&lt;6
04. 4  union
05. 5  select *
06. 6  from student
07. 7  where id&lt;4
08. 8  ;
09. 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&gt; select *
17. 2  from student
18. 3  where id&gt;2 and id&lt;6
19. 4  union all
20. 5  select *
21. 6  from student
22. 7  where id&lt;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 plain](http://blog.csdn.net/wh62592855/article/details/4712555 "view plain")[copy](http://blog.csdn.net/wh62592855/article/details/4712555 "copy")

01. SQL&gt; select score,id,name
02. 2  from student
03. 3  where id&lt;4
04. 4  union
05. 5  select score,id,name
06. 6  from student
07. 7  where id&gt;2 and id&lt;6
08. 8  ;
09. 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 plain](http://blog.csdn.net/wh62592855/article/details/4712555 "view plain")[copy](http://blog.csdn.net/wh62592855/article/details/4712555 "copy")

01. select score,id,name
02. from student
03. where id &gt; 2 and id &lt; 7
04. union
05. select score,id,name
06. from student
07. where id &lt; 4
08. union
09. select score,id,name
10. from student
11. where id &gt; 8
12. order by id desc

 

 

order by子句必须写在最后一个结果集里，并且其排序规则将改变操作后的排序结果。对于Union、Union All、Intersect、Minus都有效。

 

其他的集合操作符，如Intersect和Minus的操作和Union基本一致，这里一起总结一下：

 

Union，对两个结果集进行并集操作，不包括重复行，同时进行默认规则的排序；

Union All，对两个结果集进行并集操作，包括重复行，不进行排序；

Intersect，对两个结果集进行交集操作，不包括重复行，同时进行默认规则的排序；

Minus，对两个结果集进行差操作，不包括重复行，同时进行默认规则的排序。

 

可以在最后一个结果集中指定Order by子句改变排序方式。