您的位置:首页 > 其它

Union和Union All的区别

2013-04-07 17:16 337 查看
假设我们有一个表Student,包括以下字段与数据:

[c-sharp]
view plaincopyprint?

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;

[c-sharp]
view plaincopyprint?

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>

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?

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.

[c-sharp]
view plaincopyprint?

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.

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?

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

[c-sharp]
view plaincopyprint?

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

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子句改变排序方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: