ORACLE中Union,Intersect和Minus的等价连接查询SQL写法
来源:互联网 发布:网络教育高中升本科 编辑:程序博客网 时间:2024/04/30 12:09
有一天,在处理集合的查询SQL的时候,突发奇想,其实集合的操作是可以用连接的SQL来表示的。并根据这个假设,希望能比较一下两种SQL的查询效率。这一篇主要是用两种不同的查询SQL来同时计算不同集合运算的结果。根据测试,两种查询SQL是等价的。用来测试的表,分别是语文,英语和数学,表里面存放的是上该门课的学生,表结构如下:
create table CHINESE( TID NUMBER not null, STUDENT VARCHAR2(50))
create table ENGLISH( TID NUMBER not null, STUDENT VARCHAR2(50))
create table MATH( TID NUMBER not null, STUDENT VARCHAR2(50))
(1).A∪B∪C
<a>集合查询SQL
Select a.student from english aUnion Select b.student from chinese bUnion Select c.student from math c
Select distinct case when a.student is null then case when b.student is null then c.student else b.student end else a.student end studentfrom english a full join chinese b on a.student =b.student full join math c on a.student=c.student and a.student=b.student
(2).A∩B∩C
<a>集合查询SQL
Select a.student from english aintersect Select b.student from chinese bintersect Select c.student from math c
Select distinct a.studentfrom english a inner join chinese b on a.student =b.student inner join math c on a.student=c.student and a.student=b.student
(3).A∩B-C
<a>集合查询SQL
Select a.student from english aintersect Select b.student from chinese bminus Select c.student from math c
<b>等价连接查询SQL
Select distinct a.studentfrom chinese ainner join english b on a.student = b.studentleft join math c on a.student=c.student or b.student=c.studentwhere c.student is null
<a>集合查询SQL
Select a.student from english aunion Select b.student from chinese bintersect Select c.student from math c
<b>等价连接查询SQL
select c.studentfrom chinese afull join english b on a.student=b.student inner join math c on (a.student=c.student or b.student=c.student)
(5).A∪B-C
<a>集合查询SQL
Select a.student from english aunionSelect b.student from chinese bminusSelect c.student from math c
<b>等价连接查询SQL
Select distinct case when a.student is null then b.student else a.student endfrom chinese a full join english b on a.student = b.student left join math c on (c.student=a.student or c.student=b.student )where c.student is null
(6).C-(A∪B)
<a>集合查询SQL
Select c.student from math c minus(Select a.student from chinese aunionSelect b.student from english b)
<b>等价连接查询SQL
Select distinct c.student from chinese afull join english b on a.student=b.student right join math c on a.student=c.student or b.student=c.studentwhere a.student is null and b.student is null
(7).(A∪B∩C)-(A∩B)
<a>集合查询SQL
(Select a.student from chinese aunionSelect b.student from english bintersect Select c.student from math c)union(Select a.student from chinese aintersectSelect b.student from english b)
Select distinct case when a.student is null then case when b.student is null then c.student else b.student end else a.student end studentfrom chinese a full join english b on a.student=b.studentfull join math c on a.student=c.student or b.student =c.studentwhere (a.student is not null and b.student is not null ) or (a.student is not null and c.student is not null )or (b.student is not null and c.student is not null )
(8).(C-(A∪B))∪(C-(A∪B))∪(C-(A∪B))
<a>集合查询SQL
(Select c.student from english c minus(Select a.student from chinese aunionSelect b.student from math b))union(Select c.student from chinese c minus(Select a.student from math aunionSelect b.student from english b))union(Select c.student from math c minus(Select a.student from chinese aunionSelect b.student from english b))
<b>等价连接查询SQL
Select distinct case when a.student is null then case when b.student is null then c.student else b.student end else a.student end studentfrom chinese a full join english b on a.student=b.studentfull join math c on a.student=c.student or b.student =c.studentwhere (a.student is null and b.student is null )or (a.student is null and c.student is null )or(b.student is null and c.student is null )
对于第七和第八两种情况,如果大牛有更高效的SQL,望告知。
0 0
- ORACLE中Union,Intersect和Minus的等价连接查询SQL写法
- 常用SQL查询语句 Oracle中Union,Union All,Intersect,Minus的有什么区别
- Oracle中union、union all、minus和intersect的区别
- SQL中union, intersect 和 minus
- Oracle 合并查询union、union all、intersect和minus
- oracle中Union,Union All,Intersect和Minus操作异同
- SQL中浅谈union、union all、Intersect、Minus的区别
- Oracle中Union,Union All,Intersect,Minus
- SQL中MINUS、INTERSECT、UNION、UNION All
- Oracle中SQL语句学习三(union,Intersect,Minus)
- Oracle的union,union all ,minus,intersect
- ORACLE中MINUS,INTERSECT和UNION ALL图解
- oracle union intersect minus
- Oracle中 Union-Intersect-Minus的连合操作的使用
- oracle中 unino,union all,minus,intersect的用法
- SQL语句的MINUS,INTERSECT和UNION ALL
- SQL语句的MINUS,INTERSECT和UNION ALL
- SQL语句的MINUS,INTERSECT和UNION ALL
- ubuntu 14.04 hgfs为空
- PTAM在windows 7下的编译
- 史上最详细的centos下 postfix + extmail + dovecot + maildrop 安装笔记2014更新
- 规划法实施非格式规范价格收费价格还是帅哥哥还是帅哥好哥哥法规法规发生
- VM下RedHat linux上网配置
- ORACLE中Union,Intersect和Minus的等价连接查询SQL写法
- Mysql sql语句回顾2 --正则表达式
- 第八周项目6 百鸡问题 计算
- 事件传递之响应链(Event Delivery: The Responder Chain)[译]
- NYOJ 648 1的个数
- 《c程序设计语言》读书笔记
- jQuery Validation Engine 表单验证
- 2、bananapi使用-选用集成了MX播放器和XBMC的精简版安卓镜像
- HDOJ 题目2112 HDU Today(最短路径)