Union和Union All的区别 [转]

转 : http://blog.csdn.net/wanghai__/article/details/4712555/

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

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

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

[c-sharp] view plain copy
  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.   
  10.         ID NAME                                SCORE  
  11. ---------- ------------------------------ ----------  
  12.          1 Aaron                                  78  
  13.          2 Bill                                   76  
  14.          3 Cindy                                  89  
  15.          4 Damon                                  90  
  16.          5 Ella                                   73  
  17.   
  18. SQL>  

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

[c-sharp] view plain copy
  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.   
  10.         ID NAME                                SCORE  
  11. ---------- ------------------------------ ----------  
  12.          1 Aaron                                  78  
  13.          2 Bill                                   76  
  14.          3 Cindy                                  89  
  15.          3 Cindy                                  89  
  16.          4 Damon                                  90  
  17.          5 Ella                                   73  
  18.   
  19. 6 rows selected.  

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

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

[c-sharp] view plain copy
  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.   
  10.         ID NAME                                SCORE  
  11. ---------- ------------------------------ ----------  
  12.          1 Aaron                                  78  
  13.          2 Bill                                   76  
  14.          3 Cindy                                  89  
  15.          4 Damon                                  90  
  16.          5 Ella                                   73  
  17.   
  18. SQL> select *  
  19.   2  from student  
  20.   3  where id>2 and id<6  
  21.   4  union all  
  22.   5  select *  
  23.   6  from student  
  24.   7  where id<4  
  25.   8  ;  
  26.   
  27.         ID NAME                                SCORE  
  28. ---------- ------------------------------ ----------  
  29.          3 Cindy                                  89  
  30.          4 Damon                                  90  
  31.          5 Ella                                   73  
  32.          1 Aaron                                  78  
  33.          2 Bill                                   76  
  34.          3 Cindy                                  89  
  35.   
  36. 6 rows selected.  

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

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

[c-sharp] view plain copy
  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.   
  10.      SCORE         ID NAME  
  11. ---------- ---------- ------------------------------  
  12.         73          5 Ella  
  13.         76          2 Bill  
  14.         78          1 Aaron  
  15.         89          3 Cindy  
  16.         90          4 Damon  

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

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

[c-sharp] view plain copy
  1. select score,id,name  
  2. from student  
  3. where id > 2 and id < 7  
  4.   
  5. union  
  6.   
  7. select score,id,name  
  8. from student  
  9. where id < 4  
  10.   
  11. union  
  12.   
  13. select score,id,name  
  14. from student  
  15. where id > 8  
  16. order by id desc  

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

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

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

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

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

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

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

有关union和union all关键字需要注意的问题是:
union 和 union all都可以将多个结果集合并,而不仅仅是两个,你可以将多个结果集串起来。
使用union和union all必须保证各个select 集合的结果有相同个数的列,并且每个列的类型是一样的。但列名则不一定需要相同,oracle会将第一个结果的列名作为结果集的列名。例如下面是一个例子:
select empno,ename from emp
union
select deptno,dname from dept
我们没有必要在每一个select结果集中使用order by子句来进行排序,我们可以在最后使用一条order by来对整个结果进行排序。例如:
select empno,ename from emp
union
select deptno,dname from dept
order by ename;

猜你喜欢

转载自breezylee.iteye.com/blog/2072527