MySQL中union和union all的区别

一、区别1:取结果的并集

1、union: 对两个结果集进行并集操作, 不包括重复行,相当于distinct, 同时进行默认规则的排序;

2、union all: 对两个结果集进行并集操作, 包括重复行, 即所有的结果全部显示, 不管是不是重复;

二、区别2:获取结果后的操作

1、union: 会对获取的结果进行排序操作

2、union all: 不会对获取的结果进行排序操作

三、区别3:

1、union看到结果中ID=3的只有一条

select * from student2 where id < 4

union

select * from student2 where id > 2 and id < 6

2、union all 结果中ID=3的结果有两个

select * from student2 where id < 4

union all

select * from student2 where id > 2 and id < 6

四、总结

union all只是合并查询结果,并不会进行去重和排序操作,在没有去重的前提下,使用union all的执行效率要比union高

相关推荐
W.Buffer12 分钟前
通用:MySQL-InnoDB事务及ACID特性
数据库·mysql
he___H14 分钟前
尚庭公寓中Redis的使用
数据库·redis·缓存·尚庭公寓
菜鸟plus+2 小时前
Java 定时任务
数据库·oracle
DemonAvenger2 小时前
Redis HyperLogLog 深度解析:从原理到实战,助你优雅解决基数统计问题
数据库·redis·性能优化
潘潘潘潘潘潘潘潘潘潘潘潘2 小时前
【MySQL】库与表的基础操作
数据库·mysql·oracle
W.Buffer7 小时前
通用:MySQL-深入理解MySQL中的MVCC:原理、实现与实战价值
数据库·mysql
心态特好8 小时前
详解redis,MySQL,mongodb以及各自使用场景
redis·mysql·mongodb
一只小bit8 小时前
MySQL 库的操作:从创建配置到备份恢复
服务器·数据库·mysql·oracle
sanx188 小时前
专业电竞体育数据与系统解决方案
前端·数据库·apache·数据库开发·时序数据库