一、连接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) 连接和子查询的更多相关文章

  1. MySQL多表查询之外键、表连接、子查询、索引

    MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...

  2. ylb:SQL 表的高级查询-多表连接和子查询

    ylbtech-SQL Server: SQL Server-表的高级查询-多表连接和子查询 SQL Server 表的高级查询-多表连接和子查询. 1,ylb:表的高级查询-多表连接和子查询 返回顶 ...

  3. MySQL学习笔记——多表连接和子查询

    多表连接查询 # 返回的是两张表的乘积 SELECT * FROM tb_emp,tb_dept SELECT COUNT(*) FROM tb_emp,tb_dept # 标准写法,每个数据库都能这 ...

  4. 读《程序员的SQL金典》[3]--表连接、子查询

    一.表连接-JOIN 1. 自连接实例 查询类型相同的订单信息. SELECT O1 .*,O2.* FROM T_Order O1 JOIN T_Order O2 ON O1 .FTypeId= O ...

  5. MS sql server 基础知识回顾(二)-表连接和子查询

    五.表连接 当数据表中存在许多重复的冗余信息时,就要考虑将这些信息建在另一张新表中,在新表中为原表设置好外键,在进行数据查询的时候,就要使用到连接了,表连接就好像两根线,线的两端分别连接两张表的不同字 ...

  6. MySQL数据库学习笔记(六)----MySQL多表查询之外键、表连接、子查询、索引

    本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...

  7. HIVE:用外连接替代子查询

    由于hive也支持sql,很多人会把hql跟标准sql进行比较,甚至有的时候会直接套用.hive不支持事务也不支持索引,更不支持追加写,但是对于一般的sql都是能够支持的.但是对于一些子查询确实无法支 ...

  8. MySQL数据库学习笔记----MySQL多表查询之外键、表连接、子查询、索引

    本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...

  9. MySQL开发——【联合查询、多表连接、子查询】

    联合查询 所谓的联合查询就是将满足条件的结果进行拼接在同一张表中. 基本语法: select */字段 from 数据表1 union [all | distinct] select */字段 fro ...

随机推荐

  1. (转) VS2010 Addins 外接程序(插件)开发

    http://www.cnblogs.com/Leo_wl/archive/2013/03/21/2973886.html VS2010 Addins 外接程序(插件)开发 阅读目录 VS2010 A ...

  2. Unity3D架构设计NavMesh寻路

    Unity3D架构设计NavMesh寻路 发表于2013年10月6日由陆泽西 国庆闲来没事把NavMesh巩固一下.以Unity3D引擎为例写一个底层c# NavMesh寻路.因为Unity3D中本身 ...

  3. 认识 Thymeleaf

  4. TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add(转)

    1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...

  5. Mac中opencv批量对图片进行二值化

    对灰度图像进行二值化,传入的图片是手写汉字的截图,通过二值化把字的部分提出来.用ostu进行二值化 #include <stdio.h> #include <iostream> ...

  6. PasteDeploy部署Pecan API 服务

    part 1:请求处理 使用PasteDeploy模块来实现 WSGI Services 时,都需要加载一个 paste.ini 文件,文件用来定义服务过滤和请求路由,类似于springMvc的拦截器 ...

  7. Dao层向sql语句传递多个参数

    手动封装: serviceImpl层 Map<String, Object> params = new HashMap<String, Object>(2);params.pu ...

  8. openjdk1.8 源码地址

    http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/656ab3b39178/src/

  9. mysql不能使用IP连接,可以使用localhost连接

    问题: 本地mysql,使用127.0.0.1可以连接成功,使用具体IP连接报错 ERROR 1130 (HY000): Host '10.252.225.125' is not allowed to ...

  10. hibernate 中,出现了错误 "node to traverse cannot be null!" 如何改正

    这个错误基本上是因为hql语句写错了而造成的, 返回找hql输出一下发现, hql语句中间少了几个空格, 写成了String hql = "from"+className+&quo ...