Oracle之Union与Union all的区别
如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字。union(或称为联合)的作用是将多个结果合并在一起显示出来。
union和union all的区别是,union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复。
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union all:对两个结果集进行并集操作,包括重复行,不进行排序;
Intersect:对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
Minus:对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。
下面以实例说明Union与Union all的区别:
1、首先创建一张jack表:
SQL> create table jack
2 (
3 id int primary key,
4 name varchar2(30) not null,
5 score number not null
6 ); 表已创建。
QL> insert into jack values(1,'Aaron',78);
SQL> insert into jack values(2,'Bill',76);
SQL> insert into jack values(3,'Cindy',89);
SQL> insert into jack values(4,'Damon',90);
SQL> insert into jack values(5,'Ella',73);
SQL> insert into jack values(6,'Frado',61);
SQL> insert into jack values(7,'Gill',99);
SQL> insert into jack values(8,'Hellen',56);
SQL> insert into jack values(9,'Ivan',93);
SQL> insert into jack values(10,'Jay',90);
SQL> commit; SQL> select * from jack; ID NAME SCORE
---------- -------------------- ----------
1 Aaron 78
2 Bill 76
3 Cindy 89
4 Damon 90
5 Ella 73
6 Frado 61
7 Gill 99
8 Hellen 56
9 Ivan 93
10 Jay 90 已选择10行。
2、使用union与union all进行查询:
SQL> select * from jack where id<4
2 union
3 select * from jack where id >2 and id<6; ID NAME SCORE
---------- -------------------- ----------
1 Aaron 78
2 Bill 76
3 Cindy 89
4 Damon 90
5 Ella 73 SQL> select * from jack where id<4
2 union all
3 select * from jack where id >2 and id<6; ID NAME SCORE
---------- -------------------- ----------
1 Aaron 78
2 Bill 76
3 Cindy 89
3 Cindy 89
4 Damon 90
5 Ella 73 已选择6行。
从上面的两个查询中可以看出它们的区别之一在于对重复结果的处理。
3、调整两个子查的顺序:
SQL> select * from jack where id >2 and id<6
2 union
3 select * from jack where id<4 ; ID NAME SCORE
---------- -------------------- ----------
1 Aaron 78
2 Bill 76
3 Cindy 89
4 Damon 90
5 Ella 73 SQL> select * from jack where id >2 and id<6
2 union all
3 select * from jack where id<4 ; ID NAME SCORE
---------- -------------------- ----------
3 Cindy 89
4 Damon 90
5 Ella 73
1 Aaron 78
2 Bill 76
3 Cindy 89 已选择6行。
它们两者的区别之二在于对排序的处理。Union all将按照关联的次序组织数据,而Union将进行依据一定规则进行排序。
4、验证Union排序规则(调整一下查询字段的顺序):
SQL> select score,id,name from jack where id >2 and id<6
2 union
3 select score,id,name from jack where id<4 ; SCORE ID NAME
---------- ---------- --------------------
73 5 Ella
76 2 Bill
78 1 Aaron
89 3 Cindy
90 4 Damon
可以看到之前的查询是基于id,name,score的字段顺序,那么结果集将按照id优先进行排序;而现在新的字段顺序也改变了产讯结果的排序,由此可以按照union的排序是按照第一个字段进行排序的,但是我们也可以进行干预,指定按某个字段进行排序。
5、指定某个字段进行排序
SQL> select * from
2 (
3 select score,id,name from jack where id>2 and id<7
4 union
5 select score,id,name from jack where id<4
6 union
7 select score,id,name from jack where id>8
8 )
9 order by id desc; SCORE ID NAME
---------- ---------- --------------------
90 10 Jay
93 9 Ivan
61 6 Frado
73 5 Ella
90 4 Damon
89 3 Cindy
76 2 Bill
78 1 Aaron 已选择8行。
Oracle之Union与Union all的区别的更多相关文章
- Oracle中Union与Union All的区别(适用多个数据库)
Oracle中Union与Union All的区别(适用多个数据库) 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或 ...
- Oracle中 union 和 union all 的区别
如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字. union(或称为联合)的作用是将多个结果合并在一起显示出来. union和uni ...
- oracle中union和union all区别与性能分析
[ 概要 ] 经常写sql的同学可能会用到union和union all这两个关键词, 可能你知道使用它们可以将两个查询的结果集进行合并, 那么二者有什么区别呢? 下面我们就简单的分析下. [ 比较 ...
- 【oracle】union、union all、intersect、minus 的用法及区别
一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...
- Oracle 中 union 和union all 的简单使用说明
1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...
- union与union all 的区别
Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...
- Oracle中的Union、Union All、Intersect、Minus
Oracle中的Union.Union All.Intersect.Minus 众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括 ...
- SQL Union 和Union All 的区别
Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...
- sql中UNION和UNION ALL的区别
写sql时我们经常会遇到需要把从多张表查询的集果集进行合并.这时就用到了union.使用union或union all 时一定要保证查询的列的一致性 .不然sql会报错.字段不一致的话可以用单引号来占 ...
随机推荐
- centos 下 django 1.8 配置好后 admin 后台无法显示 样式解决办法
解决前 解决命令 [root@ayibang-server static]# cat /etc/nginx/conf.d/office_djaong_uvpv.conf server { listen ...
- Java代码中执行Linux命令,亲测可用
前提需要知道怎么在linux怎么新建java文件和怎么编译,否则请先学其他知识!! import java.io.*;public class Test{ public static void mai ...
- Power Bi的优势 特色功能
Power-BI可以让决策者不再依赖他人,就可及时.准确(没有人为加工,自然是最准确的)的得到各种关键经营数据: Power-BI不仅仅是让报表自动生成,而是完全改变了获取经营数据的方式,它可以直观的 ...
- MySQLdb模块操作
Linux 安装mysql: apt-get install mysql-server 安装python-mysql模块:apt-get install python-mysqldb Windows ...
- gradient color
http://www.cnblogs.com/YouXianMing/p/3793913.html layer 不能自动autolay, 只能在viewDidLayout里面设置宽度 - (void) ...
- ios推送
1. ios 在杀掉app后,只能接受到系统通知,JPUSH自定义消息不能接受到.系统通知经过实验只能接收到50左右个汉字. 2. 实现方案: 推送的时候,JPUSH推送一个消息,App客户端获取到数 ...
- 笔记:ASP.NET MVC安全
XSRF/CSRF Prevention in MVC ValidateAntiForgeryToken 参考这里的简单例子:http://www.asp.net/mvc/tutorials/mvc- ...
- pptp建立vpn
1. 安装依赖 ppp yum -y install ppp 2. 编译安装pptpd wget http://jaist.dl.sourceforge.net/project/poptop/pptp ...
- Troubleshooting JDK
收集整理下JDK自带的关于 Troubleshooting 的文档 Java 2 Platform, Standard Edition 5.0 Troubleshooting and Diagnost ...
- Spring 依赖注入,在Main方法中取得Spring控制的实例
Spring依赖注入机制,在Main方法中通过读取配置文件,获取Spring注入的bean实例.这种应用在实训的时候,老师曾经说过这种方法,而且学Spring入门的时候都会先学会使用如何在普通的jav ...