众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考。
假设我们有一个表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;
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、intersect、minus.docx
SQL语句生成器的特色 支持几乎所有类型的数据库, ...联合(Union,对于Oracle 支持Union All,Minus,Intersect) 支持将SQL查询语句,替换为插入(Insert into)和更新(Update)语句 附属工具内嵌入Delphi IDE
1、支持几乎所有类型的数据库, 包括小型(桌面)数据库:...联合(Union,对于Oracle 支持Union All,Minus,Intersect) 4、支持将SQL查询语句,替换为插入(Insert into)和更新(Update)语句 4、附属工具内嵌入Delphi IDE
学习oracle时的学习心得。 第一天 1 安装出现的问题: 1 运行 2 第二天 4 表管理 5 第三天 6 简单查询 7 ...union,union all,intersect,minus 17 字符函数 18 第六天 19 常用函数 19 日期函数 19 转换函数 20
支持几乎所有类型的数据库, 包括小型(桌面)数据库:Fox DBF...对于Oracle 支持Union All,Minus,Intersect) <br>支持将SQL查询语句,替换为插入(Insert into)和更新(Update)语句 附属工具内嵌入Delphi IDE
支持几乎所有类型的数据库, 包括小型(桌面)数据库:Fox ...对于Oracle 支持Union All,Minus,Intersect) <br>支持将SQL查询语句,替换为插入(Insert into)和更新(Update)语句 附属工具内嵌入Delphi IDE
4.11 集合操作符:union、intersect和minus 4.11.1 union 4.11.2 union all 4.11.3 intersect 4.11.4 minus 4.12 视图 4.13 序列 4.14 约束:与实体模型的联系、类型、延迟 4.14.1 与实体模型的联系 4.14.2 类型 ...
4.11 集合操作符:union、intersect和minus 108 4.11.1 union 109 4.11.2 union all 109 4.11.3 intersect 110 4.11.4 minus 110 4.12 视图 112 4.13 序列 113 4.14 约束:与实体模型的联系、类型、延迟 114...
联合(Union,Union All,Minus,Intersect) 4、SQL语句反向分析 无论多么复杂的语句,都能分析出来(包括SQL各子句中嵌套的SQL语句) 5、数据库视图定义和重建 6、支持将SQL查询语句,替换为插入(Insert into)和更新...
UNION ALL INTERSECT MINUS 子查询 EXISTS CASE 算排名 算中位数 算总合百分比 算累积总合百分比 SQL 语法 无论您是一位 SQL 的新手,或是一位只是需要对 SQL 复习一下的资料仓储业界老将,您 就来对地方了。这个 ...
= like between is null in逻辑运算符:not and or 集合运算符: intersect ,union, union all, minus 要求:对应集合的列数和数据类型相同 查询中不能包含long 列 列的标签是第一个集合的标签 使用order by时...
4.2.1 UNION和UNION ALL 103 4.2.2 MINUS 106 4.2.3 INTERSECT 107 4.3 集合与空值 108 4.3.1 空值与非直观结果 108 4.3.2 集合运算中的空值行为 110 4.3.3 空值与GROUP BY和ORDER BY 112 4.3.4 空值与聚合...
Oracle临时表空间主要用来做查询和存放一些缓冲区数据。临时表空间消耗的主要原因是需要对查询的中间结果进行排序。 重启数据库可以释放临时表空间,如果不能...Union 或 intersect 或 minus Sort-merge joins analyze
13.4.2 UNION、INTERSECT和MiNUS的限制 第14章 一些复杂的技术 14.1 复杂的分组 14.2 使用临时表 14.3 使用ROLLUP、GROUPING和CUBE 14.4 家族树和COlLrlectby 14.4.1 排除个体和分支 14.4.2 向根遍历 14.4.3 基本...
13.4.2 UNION、INTERSECT和MiNUS的限制 第14章 一些复杂的技术 14.1 复杂的分组 14.2 使用临时表 14.3 使用ROLLUP、GROUPING和CUBE 14.4 家族树和COlLrlectby 14.4.1 排除个体和分支 14.4.2 向根遍历 14.4.3 基本...
ORACLE常用命令 一、ORACLE的启动和关闭 1、在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 su - oracle a、启动ORACLE系统 oracle>svrmgrl SVRMGR>connect internal SVRMGR>startup ...