union all和union的区别

假设我们有一个表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的例子:

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是把两个集合进行并集,但是把重复的数据只留一个。

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

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.  

换成了union all 之后发现结果中有重复,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 ID,NAME,SCORE),看看结果如何:

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呢?当然可以。不过在写法上有需要注意的地方:

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子句改变排序方式。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unionunion all是SQL中用于合并两个结果集的操作符,它们的区别主要体现在以下两个方面: 1. 返回结果集的唯一性: - union操作符会返回两个结果集的并集,并且会自动去除重复的记录,保留唯一的记录。 - union all操作符会返回两个结果集的并集,不会去除重复的记录,保留所有的记录,包括重复的记录。 2. 排序和性能: - union操作符会按照字段的顺序进行排序,确保返回的结果集是有序的。 - union all操作符只是简单地将两个结果集合并后返回,不进行排序操作。因此,从性能上讲,union all比union,特别是当两个结果集中没有重复记录且不需要排序时,推荐使用union all。 下面是一个示例,演示了unionunion all的区别: ```sql -- 创建两个表 CREATE TABLE table1 ( id INT, name VARCHAR(10) ); CREATE TABLE table2 ( id INT, name VARCHAR(10) ); -- 向表中插入数据 INSERT INTO table1 (id, name) VALUES (1, 'A'); INSERT INTO table1 (id, name) VALUES (2, 'B'); INSERT INTO table1 (id, name) VALUES (3, 'C'); INSERT INTO table2 (id, name) VALUES (2, 'B'); INSERT INTO table2 (id, name) VALUES (3, 'C'); INSERT INTO table2 (id, name) VALUES (4, 'D'); -- 使用union操作符合并结果集 SELECT id, name FROM table1 UNION SELECT id, name FROM table2; -- 输出结果:(去除了重复记录) -- id | name --+----- -- 1 | A -- 2 | B -- 3 | C -- 4 | D -- 使用union all操作符合并结果集 SELECT id, name FROM table1 UNION ALL SELECT id, name FROM table2; -- 输出结果:(保留了重复记录) -- id | name --+----- -- 1 | A -- 2 | B -- 3 | C -- 2 | B -- 3 | C -- 4 | D ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值