日期:2014-05-16  浏览次数:20497 次

Union和Union All的区别(l转)

?

文章出自: http://blog.csdn.net/wh62592855/article/details/4712555/

?

Union和Union All的区别

?

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

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的例子:

?

select *
  from student
 where id < 4
union
select *
  from student
 where id > 2
   and id < 6;

?结果:

?

?

?

        ID NAME                                SCORE
---------- ------------------------------ ----------
         1 Aaron                                  78
         2 Bill                                   76
         3 Cindy                                  89
         4 Damon                                  90
         5 Ella                                   73

?

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

select *
  from student
 where id < 4
union all
select *
  from student
 where id > 2
   and id < 6;

?

结果:

        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语句的顺序,看看结果是怎样的。

?

?

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