经常写sql的同学可能会用到union和union all这两个关键词, 可能你知道使用它们可以将两个查询的结果集进行合并,
那么二者有什么区别呢? 下面我们就简单的分析下.
[ 比较 ]
union: 对两个结果集进行并集操作, 不包括重复行,相当于distinct, 同时进行默认规则的排序;
union all: 对两个结果集进行并集操作, 包括重复行, 即所有的结果全部显示, 不管是不是重复;
下面我们举一个简单的例子来证明上面的结论:
1. 准备数据:
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;
2. 比较不同点
查询比较①
-- union all select * from student where id < 4 union all select * from student where id > 2 and id < 6 -- union select * from student where id < 4 union select * from student where id > 2 and id < 6
union all 查询结果:
union 查询结果:
通过比较不难看出, union all不会去掉重复记录, 而union去掉了重复的记录.
查询比较②
-- union all select * from student where id > 2 and id < 6 union all select * from student where id < 4 -- union select * from student where id > 2 and id < 6 union select * from student where id < 4
更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/database/Oracle/
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索insert
, select
, union
, union all
, student
, values
, into
去掉重复行
oracle union all、oracle union all用法、oracle中union all、oracle sql union all、oracle中的union all,以便于您获取更多的相关知识。