Oracle中的Union、Union All、Intersect、Minus
来源:互联网 发布:多目标优化遗传算法 编辑:程序博客网 时间:2024/05/16 10:33
众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考。 假设我们有一个表Student,包括以下字段与数据: drop table student; create table student insert into student values(1,'Aaron',78); commit; select * union select * 结果将是 1 Aaron 78 如果换成Union All连接两个结果集,则返回结果是: 1 Aaron 78 可以看到,Union和Union All的区别之一在于对重复结果的处理。 接下来我们将两个子查询的顺序调整一下,改为 --Union select * union select * 看看执行结果是否和你期望的一致? --Union All select * union all select * 那么这个呢? 据此我们可知,区别之二在于对排序的处理。Union All将按照关联的次序组织数据,而Union将进行依据一定规则进行排序。那么这个规则是?我们换个查询方式看看: select score,id,name union select score,id,name 结果如下: 73 5 Ella 和我们预料的一致:将会按照字段的顺序进行排序。之前我们的查询是基于id,name,score的字段顺序,那么结果集将按照id优先进行排序;而现在新的字段顺序也改变了查询结果的排序。并且,是按照给定字段a,b,c...的顺序进行的order by。即结果是order by a,b,c...........的。我们看下一个查询: select score,id,name union select score,id,name 结果如下: 56 8 Hellen 可以看到,对于score相同的记录,将按照下一个字段id进行排序。如果我们想自行控制排序,是不是用order by指定就可以了呢?答案是肯定的,不过在写法上有需要注意的地方: select score,id,name union select score,id,name union select score,id,name order by子句必须写在最后一个结果集里,并且其排序规则将改变操作后的排序结果。对于Union、Union All、Intersect、Minus都有效。 ================================================================================================================= Intersect和Minus的操作和Union基本一致,这里一起总结一下: Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序; Union All,对两个结果集进行并集操作,包括重复行,不进行排序; Intersect,对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序; Minus,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。 可以在最后一个结果集中指定Order by子句改变排序方式。
(
id int primary key,
name nvarchar2(50) not null,
score number not null
);
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);
from student
where id < 4
from student
where id > 2 and id < 6
2 Bill 76
3 Cindy 89
4 Damon 90
5 Ella 73
2 Bill 76
3 Cindy 89
3 Cindy 89
4 Damon 90
5 Ella 73
from student
where id > 2 and id < 6
from student
where id < 4
from student
where id > 2 and id < 6
from student
where id < 4
from student
where id > 2 and id < 6
from student
where id < 4
76 2 Bill
78 1 Aaron
89 3 Cindy
90 4 Damon
from student
where id > 2
from student
where id < 4
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
from student
where id > 2 and id < 7
from student
where id < 4
from student
where id > 8
order by id desc
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle 中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- Oracle中的Union、Union All、Intersect、Minus
- 编程之路┊一个程序员走过的路
- GridView根据值的变化改变行列样式
- Heartbeat + LVS + ldirectord构建可伸缩网络服务(1)
- MSCOMM 串口控件的基本使用
- c语言中的offsetof()宏
- Oracle中的Union、Union All、Intersect、Minus
- 调试工具Firebug的使用方法
- start with 和 connect by 的用法
- 枚举Windows窗口
- Windows Live Writer 写博客还挺方便
- 一个完整的AjaxPro例子
- CERL 2.0 预告:Erlang Style Concurrency + 状态机
- SMARTARM2200 ADS工程在IAR EWARM 5.3上的移植(6)-LwIP 1.2的移植(uCOSII部分)
- 将数据导出到excel与日期格式的设置