Union和Union All的区别
2017-08-11 10:29
190 查看
假设我们有一个表Student,包括以下字段与数据:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
首先,我们来看一下UNION的例子:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
如果换成Union All连接两个结果集,则结果如下:
区别二:处理结果的顺序
接下来,我们交换一个两个SELECT语句的顺序,看看结果是怎样的。
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
可以看到,对于UNION来说,交换两个SELECT语句的顺序后结果仍然是一样的,这是因为UNION会自动排序。而UNION ALL在交换了SELECT语句的顺序后结果则不相同,因为UNION ALL不会对结果自动进行排序。
排序规则
那么这个自动排序的规则是什么呢?我们交换一下SELECT后面选择字段的顺序(前面使用SELECT *相当于SELECT ID,NAME,SCORE),看看结果如何:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
可是看到,此时是按照字段SCORE来对结果进行排序的(前面SELECT *的时候是按照ID进行排序的)。
那么有人会问,如果我想自行控制排序,能不能使用ORDER BY呢?当然可以。不过在写法上有需要注意的地方:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
order by子句必须写在最后一个结果集里,并且其排序规则将改变操作后的排序结果。
-----------------------------------------------------------精华在这里:-----------------------------------------------------------------
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
01.drop table student; 02. 03.create table student 04.( 05.id int primary key, 06.name nvarchar2(50) not null, 07.score number not null 08.); 09. 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的例子:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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连接两个结果集,则结果如下:
区别二:处理结果的顺序
接下来,我们交换一个两个SELECT语句的顺序,看看结果是怎样的。
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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
可以看到,对于UNION来说,交换两个SELECT语句的顺序后结果仍然是一样的,这是因为UNION会自动排序。而UNION ALL在交换了SELECT语句的顺序后结果则不相同,因为UNION ALL不会对结果自动进行排序。
排序规则
那么这个自动排序的规则是什么呢?我们交换一下SELECT后面选择字段的顺序(前面使用SELECT *相当于SELECT ID,NAME,SCORE),看看结果如何:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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呢?当然可以。不过在写法上有需要注意的地方:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。 可以在最后一个结果集中指定Order by子句改变排序方式。
相关文章推荐
- union和union all的区别
- union和union all的区别
- 图解SQL的inner join、left join、right join、full outer join、union、union all的区别
- union和union all的区别
- 组合查询中union 与union all的区别
- Union 与 Union all 区别
- UNION和UNION ALL的用法区别
- Union和Union All的区别
- Union和Union All的区别
- [SQL] Union和Union All的区别
- union和union all的区别
- union和union all的区别
- UNION和UNION ALL的区别
- Oracle中Union与Union All的区别(适用多个数据库)
- UNION 和UNION ALL两者之间的内在区别
- Union与Union All的区别
- union和union All的区别
- Union和Union All的区别
- Union 和 union all 的区别
- Union 和Union all的区别