您的位置:首页 > 数据库 > Oracle

oracle中union和union all

2020-06-06 07:14 344 查看

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

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

实际证明

1、准备对应的数据

create table demo(
id int primary key not null,
dname varchar2(50) not null,
age varchar2(50) not null
);

insert into demo values(1,'t1','1');
insert into demo values(2,'t2','2');
insert into demo values(3,'t3','3');
insert into demo values(4,'t4','4');
insert into demo values(5,'t5','5');

2、union和union all进行比较

select * from demo d where d.id > 1
union
select * from demo d where d.id > 4;

select * from demo d where d.id > 1
union all
select * from demo d where d.id > 4;

union的执行结果

union all的执行结果

总结
 union: 对两个结果集进行并集操作, 不包括重复行,相当于distinct, 同时进行默认规则的排序,效率不高,因为他对查询的结果集进行了排序和去重
 union all: 对两个结果集进行并集操作, 包括重复行, 即所有的结果全部显示, 不管是不是重复,效率高

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: