union all和union的区别

union的默认排序规则见下文

数据库:mysql

实例

 
create table test  
(  
id int primary key,  
name varchar(50) not null,  
score int not null  
); 
  
 
insert into test values(1,'Aaron',78);  
insert into test values(2,'Bill',76);  
insert into test values(3,'Cindy',89);  
insert into test values(4,'Damon',90);  
insert into test values(5,'Ella',73);  
insert into test values(6,'Frado',61);  
insert into test values(7,'Gill',99);  
insert into test values(8,'Hellen',56);  
insert into test values(9,'Ivan',93);  
insert into test values(10,'Jay',90); 

1.1执行union

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

如图:

1.2换下select执行顺序: 

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

结果如图:

总结:可以看到,对于UNION来说,交换两个SELECT语句的顺序后结果仍然是一样的,这是因为UNION会自动排序

 2.1执行union all

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

如图:

2.2换下select的顺序:

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

如图:

总结:UNION ALL在交换了SELECT语句的顺序后结果则不相同,因为UNION ALL不会对结果自动进行排序。

扩展:

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

select score,id,name  
    from student  
    where id<4  
    union  
    select score,id,name  
    from student  
    where id>2 and id<6  
    ;

执行结果:

可以看出,默认是根据主键升序排序

特别注意:如果id不是主键,会根据score字段排序;

unoin也可以自定义排序,指定某个字段升序或是降序,如下:

select score,id,name  
from test  
where id > 2 and id < 7  
  
union  
  
select score,id,name  
from test  
where id < 4  
  
union  
  
select score,id,name  
from test  
where id > 8  
order by score desc  

 

运行结果如下:

文章来自:union all和union的区别_麦子的博客-CSDN博客_unionall 

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值