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: ...
随机推荐
- python模拟浏览器文件上传,csrf放行
服务器端视图函数 from django.shortcuts import render,HttpResponse from django.views.decorators.csrf import c ...
- 关系数据库理论 ch.6
6.1 问题的提出 关系模式是一个5元组 R U,D,DOM,F U 属性 D 域 DOM 属性到域的映射 F 依赖 在本章中将关系模式看作 三元组 R U,D 属性-依赖 1NF 每一个分量是不可分 ...
- I Love Palindrome String
I Love Palindrome String 时间限制: 2 Sec 内存限制: 128 MB 题目描述 You are given a string S=s1s2..s|S| containi ...
- (转)Json在Unity中的简单使用
Json数据解析在Unity3d中的应用 最近做项目过程中因为Json文件名写错了一个字母Unity报错,找错误找到半夜,当时为了验错,写了一个小Demo,正好借此总结一下Json. 1.什么是Jso ...
- 关于Layui 响应式移动端轮播图的问题
用layui做轮播图,在手机上宽度异常, 可通过以下方法解决, 不喜欢layui的同学可以选择Swiper // 轮播图 layui.use('carousel', function () { var ...
- jquery 调用asp.net后台代码
1.需要引用对应的命名空间 System.Web.Services 2.后台方法: 必须是static 约束 必须添加[WebMethod()] 属性 示例: <script type=&q ...
- CSS hack处理
css hack指各版本及各品牌浏览器之间对CSS解释后出现网页内容的误差. 各浏览器CSS解析: 1.大部分特殊字符IE浏览器支持,其他主流浏览器firefox,chrome,opera,safar ...
- elipse egit的使用
- Linux SSH远程链接 短时间内断开
Linux SSH远程链接 短时间内断开 操作系统:RedHat 7.5 问题描述: 在进行SSH链接后,时不时的就断开了 解决方案: 修改 /etc/ssh/sshd_config 文件,找到 Cl ...
- Core Data Migration 之拆分Entity
参考文章:http://blog.slalom.com/2010/01/08/splitting-an-entity-in-a-coredata-migration/ 最近自己做的背单词软件,在之前的 ...