Oracle_SQL(5) 连接和子查询
一、连接join
一般分类:
inner join: 内连接,又叫等值连接,只返回两个表中连接字段相等的行。
left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录。
right join :右连接,返回右表中所有的记录以及左表中连接字段相等的记录。
full join:外连接,返回两个表中的行:left join + right join。
cross join:笛卡尔积,就是第一个表的行数乘以第二个表的行数。
oracle分类:等值连接 =
外连接(左外连接、右外连接、全连接)=(+)
笛卡尔积
update emp set deptno=null where empno=7839;
1.内连接\等值连接
select emp.*,dept.* from emp,dept where emp.deptno=dept.deptno;
select emp.*,dept.*
from emp inner join dept on emp.deptno=dept.deptno;
2.左连接\左外连接
select emp.*,dept.* from emp,dept where emp.deptno=dept.deptno(+);
select emp.*,dept.*
from emp left join dept on emp.deptno=dept.deptno;
3.右连接\右外连接
select emp.*,dept.* from emp,dept where emp.deptno(+)=dept.deptno;
select emp.*,dept.*
from emp right join dept on emp.deptno=dept.deptno;
4.外连接\全连接
select emp.*,dept.* from emp,dept where emp.deptno=dept.deptno(+)
union
select emp.*,dept.* from emp,dept where emp.deptno(+)=dept.deptno;
select emp.*,dept.*
from emp full join dept on emp.deptno=dept.deptno;
5.笛卡尔积
select emp.*,dept.* from emp,dept;
select emp.*,dept.* from emp cross join dept;
二、子查询
在查询语句的select、from、where部分再嵌入一个查询语句,
这个被嵌入的查询语句称为子查询。
子查询可以被多层嵌套。
1.select子查询
select emp.*,
(select dname from dept where dept.deptno=emp.deptno) as dname
from emp;
2.from子查询
select emp.*,dept1.* from emp,
(select * from dept where dname in ('RESEARCH','SALES')) dept1
where dept1.deptno=emp.deptno;
3.where子查询
select emp.* from emp where emp.deptno in
(select deptno from dept where dname in ('RESEARCH','SALES'));
select emp.* from emp where exists
(select 1 from dept where dept.deptno=emp.deptno
and dname in ('RESEARCH','SALES'));
4.对应的连接查询
select emp.*,dept.* from emp,dept
where dept.deptno=emp.deptno and dname in ('RESEARCH','SALES');
5.总结:一般连接查询效率是最高,但受到数据质量的限制比较多,
且不容易写出sql或sql的健壮性不足。
比如:要查出所有职员的部门名称,用连接查询时需要考虑每个职员是否有部门编号,
如果都有部门编号时,可以用等值连接书写;
如果存在没有部门编号的职员时,就要考虑用外连接,至于时左外连接还是右外连接还和职员表与部门表的位置有关;
这就增加的书写sql的难度,并增加的错误出现的概率。
用select子查询示例的方式书写就不用考虑这些问题,简化的编写sql的难度,
且不用考虑数据的真实情况。
但如果要查询的字段比较多时,select子查询示例的方式又比较低效,
所以需要综合考虑。
在结果正确的前提下考虑sql程序的健壮性、效率。
三、谓词查询
0.in 某列的值属于集合成员中的一个成员
select emp.* from emp where emp.deptno in
(select deptno from dept where dname in ('RESEARCH','SALES'));
1.exists 总存在一个值满足条件
select emp.* from emp where exists
(select 1 from dept where dept.deptno=emp.deptno
and dname in ('RESEARCH','SALES'));
2.not exists 不存在任何值满足条件
select emp.* from emp where not exists
(select 1 from dept where dept.deptno=emp.deptno
and dname in ('RESEARCH','SALES'));
3.any 某列的值满足一个条件即可
select emp.* from emp where emp.deptno = any
(select deptno from dept where dname in ('RESEARCH','SALES'));
4.some 满足集合中的某些值
select emp.* from emp where emp.deptno = some
(select deptno from dept where dname in ('RESEARCH','SALES'));
5.all 某列的值满足子查询中所有值的记录
select emp.* from emp where emp.deptno = all
(select deptno from dept where dname in ('RESEARCH','SALES'));
select * from emp where emp.deptno <
all(select deptno from dept where dname in ('RESEARCH','SALES'));
6.总结:
exists、any、some与in的功能是类似的,not exists、all与not in功能类似;
any、some、all比起in来不方便理解,基本不在实际中使用;
exists和not exists要比in和not in效率高,所以需要重点掌握。
如果对exists和not exists理解有困难的话,
可以先用in和not in写出sql,在按照格式修改为exists和not exists即可。
四、查询结果的并、交、差
1.union 并(去重)
select emp.*,dept.* from emp,dept where emp.deptno=dept.deptno(+)
union
select emp.*,dept.* from emp,dept where emp.deptno(+)=dept.deptno;
2.union all 并(不去重)
select emp.*,dept.* from emp,dept where emp.deptno=dept.deptno(+)
union all
select emp.*,dept.* from emp,dept where emp.deptno(+)=dept.deptno;
3.intersect 交
select emp.*,dept.* from emp,dept where emp.deptno=dept.deptno(+)
intersect
select emp.*,dept.* from emp,dept where emp.deptno(+)=dept.deptno;
等价与等值连接
select emp.*,dept.* from emp,dept where emp.deptno=dept.deptno;
4.minus 差
select emp.*,dept.* from emp,dept where emp.deptno=dept.deptno(+)
minus
select emp.*,dept.* from emp,dept where emp.deptno(+)=dept.deptno;
差操作的结果的两个查询的位置有关:
select emp.*,dept.* from emp,dept where emp.deptno(+)=dept.deptno
minus
select emp.*,dept.* from emp,dept where emp.deptno=dept.deptno(+);
五、子查询对增删改功能的增强
1.批量新增
语法:insert into <表名> (列名,...) 子查询;
insert into dept
(select empno/100 as deptno,ename as dname,ename as loc
from emp where job='MANAGER');
2.批量修改
语法:update <表名> set <列名1>=<表达式1>,... where 子查询;
update emp set sal=sal+500 where deptno in
(select deptno from dept where dname in ('RESEARCH','SALES'));
3.批量删除
语法:delete from <表名> where 子查询;
delete from emp where emp.deptno in
(select deptno from dept where dname in ('RESEARCH','SALES'));
Oracle_SQL(5) 连接和子查询的更多相关文章
- MySQL多表查询之外键、表连接、子查询、索引
MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...
- ylb:SQL 表的高级查询-多表连接和子查询
ylbtech-SQL Server: SQL Server-表的高级查询-多表连接和子查询 SQL Server 表的高级查询-多表连接和子查询. 1,ylb:表的高级查询-多表连接和子查询 返回顶 ...
- MySQL学习笔记——多表连接和子查询
多表连接查询 # 返回的是两张表的乘积 SELECT * FROM tb_emp,tb_dept SELECT COUNT(*) FROM tb_emp,tb_dept # 标准写法,每个数据库都能这 ...
- 读《程序员的SQL金典》[3]--表连接、子查询
一.表连接-JOIN 1. 自连接实例 查询类型相同的订单信息. SELECT O1 .*,O2.* FROM T_Order O1 JOIN T_Order O2 ON O1 .FTypeId= O ...
- MS sql server 基础知识回顾(二)-表连接和子查询
五.表连接 当数据表中存在许多重复的冗余信息时,就要考虑将这些信息建在另一张新表中,在新表中为原表设置好外键,在进行数据查询的时候,就要使用到连接了,表连接就好像两根线,线的两端分别连接两张表的不同字 ...
- MySQL数据库学习笔记(六)----MySQL多表查询之外键、表连接、子查询、索引
本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...
- HIVE:用外连接替代子查询
由于hive也支持sql,很多人会把hql跟标准sql进行比较,甚至有的时候会直接套用.hive不支持事务也不支持索引,更不支持追加写,但是对于一般的sql都是能够支持的.但是对于一些子查询确实无法支 ...
- MySQL数据库学习笔记----MySQL多表查询之外键、表连接、子查询、索引
本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...
- MySQL开发——【联合查询、多表连接、子查询】
联合查询 所谓的联合查询就是将满足条件的结果进行拼接在同一张表中. 基本语法: select */字段 from 数据表1 union [all | distinct] select */字段 fro ...
随机推荐
- SQL调用C# dll(第二中DLL,强名称密匙)
参考:微软官网 https://msdn.microsoft.com/zh-cn/library/ms345106(es-es).aspx 1.新建项目 SQLDllTestUsingNew Clas ...
- python 稀疏向量和矩阵的表示形式
http://blog.csdn.net/nkwangjie/article/details/17502443 http://blog.csdn.net/bitcarmanlee/article/de ...
- js中将类数组转换为数组的几种方法
1.slice方法 最经典的方法,使用Array的slice方法,此方法如果不传参数的话会返回原数组的一个拷贝,因此可以用此方法转换类数组到数组: // 创建一个类数组对象 var alo = {0: ...
- ArrayList删除--------ConcurrentModificationException问题
在做项目中用到List存储数据,在里面做数据操作时候用到了删除.结果抛出ConcurrentModificationException异常.在这里把问题总结一下. 原因: ArrayList进行for ...
- linux安装node简单方法
1.去官网下载和自己系统匹配的文件: 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通过 uname -a ...
- 使用openshit在ubuntu14.04下一键部署openstack(juno版本)
一.基本介绍 本实验是在vmware workstation上虚拟机ubuntu14.04(64bit,desktop)上部署openstack(Juno版本).采用的工具是openshit.open ...
- CSSの変数を使う
この文章はhttps://developer.mozilla.org/ja/docs/Web/CSS/Using_CSS_variablesを参考します. これは実験段階の機能です.この機能は複数のブ ...
- sql数据库之多库查询
连接到数据库服务器gwsps07上,打开查询分析器,如何获取gwrenshi数据库中的数据? 查询语句如下: select * from GWRENSHI.CGC.dbo.PERempms(serve ...
- Jenkins+svn+ant+tomcat持续集成
转载自 http://www.cnblogs.com/liuhaixia/p/7267473.html Jenkins是基于Java开发的一种持续集成工具,用于监控秩序重复的工作.通过Jenkins+ ...
- php拓展
https://github.com/phalcon/zephirhttp://blog.csdn.net/black_OX/article/details/43700707