Intersect,Minus,union all 和union的区别
来源:互联网 发布:初学英语入门自学软件 编辑:程序博客网 时间:2024/05/21 06:54
Union因为要进行重复值扫描,所以效率低。如果合并没有刻意要删除重复行,那么就使用Union All
两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致);
如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字。union(或称为联合)的作用是将多个结果合并在一起显示出来。
union和union all的区别是,union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复。
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union All:对两个结果集进行并集操作,包括重复行,不进行排序;
Intersect:对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
Minus:对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。
可以在最后一个结果集中指定Order by子句改变排序方式。
例如:
select employee_id,job_id from employees
union
select employee_id,job_id from job_history
以上将两个表的结果联合在一起。这两个例子会将两个select语句的结果中的重复值进行压缩,也就是结果的数据并不是两条结果的条数的和。如果希望即使重复的结果显示出来可以使用union all,例如:
2.在oracle的scott用户中有表emp
select * from emp where deptno >= 20
union all
select * from emp where deptno <= 30
这里的结果就有很多重复值了。
有关union和union all关键字需要注意的问题是:
union 和 union all都可以将多个结果集合并,而不仅仅是两个,你可以将多个结果集串起来。
使用union和union all必须保证各个select 集合的结果有相同个数的列,并且每个列的类型是一样的。但列名则不一定需要相同,oracle会将第一个结果的列名作为结果集的列名。例如下面是一个例子:
select empno,ename from emp
union
select deptno,dname from dept
我们没有必要在每一个select结果集中使用order by子句来进行排序,我们可以在最后使用一条order by来对整个结果进行排序。例如:
select empno,ename from emp
union
select deptno,dname from dept
order by ename;
- Oracle中union、union all、minus和intersect的区别
- Union和Union All、Intersect、Minus的区别
- Intersect,Minus,union all 和union的区别
- Oracle Union/Union All/Intersect/Minus区别
- union 和union all 区别,Intersect、 Minus 函数介绍
- SQL中浅谈union、union all、Intersect、Minus的区别
- INTERSECT MINUS UNION和UNION ALL
- 数据库的Union、Union All、Intersect、Minus
- 数据库的Union、Union All、Intersect、Minus
- Oracle的union,union all ,minus,intersect
- Union, Union all, intersect, minus
- union、union all、intersect、minus
- Union、Union All、Intersect、Minus
- UNION ,UNION ALL ,INTERSECT ,MINUS
- minus,union,union all,intersect 这几个的区别和用法
- MINUS,INTERSECT和UNION ALL比较
- SQL语句的MINUS,INTERSECT和UNION ALL
- SQL语句的MINUS,INTERSECT和UNION ALL
- python 常用string函数
- zookeeper使用和原理探究
- 冒泡排序---易懂
- java 数据结构经典算法
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- Intersect,Minus,union all 和union的区别
- C# 设置properity 使Form的边框固定
- 笔记79--ImageSwitcher用法
- Android开发实践:JNI层线程回调Java函数示例
- android中listview卡顿的终极原因解析
- hadoop-hive-DISTINCT
- Ubuntu简单用法
- Android permission 访问权限大全
- 第七周思考并验证项目一