Oracle中的Union、Union All、Intersect、Minus[转]
众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考。
假设我们有一个表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和Union All的区别。
select * from student where id < 4
union
select * from student where id > 2 and id < 6
结果将是
1 Aaron 78 2 Bill 76 3 Cindy 89 4 Damon 90 5 Ella 73
如果换成Union All连接两个结果集,则返回结果是:
1 Aaron 78 2 Bill 76 3 Cindy 89 3 Cindy 89 4 Damon 90 5 Ella 73
可以看到,Union和Union All的区别之一在于对重复结果的处理。
接下来我们将两个子查询的顺序调整一下,改为
--Union
select * from student where id > 2 and id < 6
union
select * from student where id < 4
看看执行结果是否和你期望的一致?
--Union All
select * from student where id > 2 and id < 6
union all
select * from student where id < 4
那么这个呢?
据此我们可知,区别之二在于对排序的处理。Union All将按照关联的次序组织数据,而Union将进行依据一定规则进行排序。那么这个规则是?我们换个查询方式看看:
select score,id,name from student where id > 2 and id < 6
union
select score,id,name from student where id < 4
结果如下:
73 5 Ella 76 2 Bill 78 1 Aaron 89 3 Cindy 90 4 Damon
和我们预料的一致:将会按照字段的顺序进行排序。之前我们的查询是基于id,name,score的字段顺序,那么结果集将按照id优先进行排序;而现在新的字段顺序也改变了查询结果的排序。并且,是按照给定字段a,b,c...的顺序进行的order by。即结果是order by a,b,c...........的。我们看下一个查询:
select score,id,name from student where id > 2
union
select score,id,name from student where id < 4
结果如下:
56 8 Hellen 61 6 Frado 73 5 Ella 76 2 Bill 78 1 Aaron 89 3 Cindy 90 4 Damon 90 10 Jay 93 9 Ivan 99 7 Gill
可以看到,对于score相同的记录,将按照下一个字段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子句改变排序方式
Oracle中的Union、Union All、Intersect、Minus[转]的更多相关文章
- Oracle中的Union、Union All、Intersect、Minus
Oracle中的Union.Union All.Intersect.Minus 众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括 ...
- ORACLE中union/union all/Intersect/Minus用法
Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All,对两个结果集进行并集操作,包括重复行,不进行排序: Intersect,对两个结果集进行交集操作,不包 ...
- Oracle集合操作函数:union、intersect、minus
[转]Oracle集合操作函数:union.intersect.minus 集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINU ...
- 【转】Oracle集合操作函数:union、intersect、minus
集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配. 集合操作符 ...
- oracle 的交并差函数,intersect;union;minus。
创建表并添加数据: --创建TABLE_A create table TABLE_A ( A ), B ) ); --给TABLE_A添加数据 insert into TABLE_A values(' ...
- Oracle SQL Lesson (8) - 使用集合操作符(Union,Intersect,Minus)
集合操作符UNION/UNION ALLINTERSECTMINUS Union All不排序,不去重,其余均升序且去重.create table e1 as select * from emp wh ...
- Oracle中Union与Union All的区别(适用多个数据库)
Oracle中Union与Union All的区别(适用多个数据库) 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或 ...
- Union、Union All、Intersect、Minus用法和区别
假设我们有一个表Student,包括以下字段与数据: [c-sharp] view plain copydrop table student; create table student ( ...
- Union、Union All、Intersect、Minus
转自:http://www.2cto.com/database/201208/148795.html Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All: ...
随机推荐
- Codeforces 463D
题目链接 D. Gargari and Permutations time limit per test 2 seconds memory limit per test 256 megabytes i ...
- Codeforces 222B 数组行列交换操作
/*做完这题发现自己好水,太伤人了.... 不过还是学到一些,如果直接暴力模拟的话肯定是TLM.. 所以要用虚拟数组来分别保存当前数组的每行没列在初始数组中的位置...*/ #include<c ...
- java swing多线程
比如一个爬虫 在界面上显示当前时间,每秒都刷新一次用来判断软件是不是卡死 在爬取程序运行的时候,界面可能会卡死 那这就要把爬取程序放在另一个线程里边 同时,也可以把rtc放在另一个线程里边 具体代码, ...
- spring基于xml的声明式事务控制配置步骤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- python基础--常用的模块(collections、time、datetime、random、os、sys、json、pickle)
collection模块: namedtuple:它是一个函数,是用来创建一个自定义的tuple对象的,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素.所以我们就可以 ...
- 2019.9.24 csp-s模拟测试51(a) 反思总结
T1:还在头铁,顺便复习了一下lct[虽然这题用不上因为复杂度不对] 头铁结束. 虽然题目存在换根的操作,实际上并不用真的换根. 2操作中求lca的时候只要考虑原树上root和x.y的lca以及x,y ...
- python实例 函数
#! /usr/bin/python # -*- coding: utf8 -*- def sum(a,b): return a+b func = sum r = func(5,6) prin ...
- 洛谷P1084 [NOIP2012提高组Day2T3]疫情控制
P1084 疫情控制 题目描述 H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点. H 国的首都爆发了一种危害性极高的传染病.当局为了控 ...
- Leetcode47. Permutations II全排列2
给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 在全排列1题目的基础上先排序,目的是把相同的 ...
- golang之switch