SQL.Cookbook 读书笔记3 操作多个表
第三章 操作多个表
表连接的内连接和外连接
A表 B表
id name id name
1 a 1 b
2 b 3 c
4 c
内连接就是左表和右表相同的数据,查询结果只有相等的数据:
select * from A inner join B on A.id=B.id
select * from A,B where A.id=B.id
id name id name
1 a 1 b
外连接分为:左外连接、右外连接、全外连接
左外连接就是以左表为准,去匹配右表,左表有多少条数据,结果就是多少条数据
select * from A left join B on A.id=B.id
id name id name
1 a 1 b
2 b null null
4 c null null
右外连接就是与左外连接反之,以右表为准,去匹配左表,右表有多少条数据,结果就是多少条数据
select * from A right join B on A.id=B.id
id name id name
1 a 1 b
null null 3 c
全外连接数据条数不一定,相当与是左外连接 和右外连接 的综合
select * from A full join B on A.id=B.id
id name id name
1 a 1 b
2 b null null
null null 3 c
4 c null null
3.1 表链接
select e.ename,d.loc from emp e,dept d where e.deptno=d.deptno and e.deptno= ;
这是等值连接是内链接的一种 还有另一种写法
select e.ename,d.loc from emp e inner join dept d on (e.deptno=d.deptno)where e.deptno = ;
3.2 从一个表中查询另一个表中没有的值 这个相当于两个查询没有表连接
MYSQL
select distinct deptno from dept where dept not in (select deptno from emp);
ORACLE
select deptno from dept minus select deptno from emp;
3.3 在表中查找与其他表不匹配的记录 两个表外连接取出连接后A表中有值B表中没值的记录
MYSQL
select d.* from dept d left outer join emp e on (d.deptno=e.deptno) where e.deptno is null;
ORACLE
select d.* from dept d ,emp e where d.deptno = e.deptno (+) and e.deptno is null; -- oracle 的左连接的写法
3.4 向查询中添加连接又不影响其他链接 内连接外加外连接 不会因为第二个连接造成第一个链接结果改变
MYSQL
select e.ename,d.loc,eb.received from emp e join dept d on (e.deptno=d.deptno) left join emp_bonus eb on(e.empno=eb.empno) order by ;
ORACLE
select e.ename,d.loc,eb.received from emp e , dept d , emp_bonus eb where e.deptno=d.deptno and e.empno = eb.empno (+) order by 2;
标量子查询
select e.ename ,d.loc,(select eb.received from emp_bonus eb where eb.empno=e.empno)as received from emp e,dept d where e.deptno= d.deptno order by 2;
3.5 聚集与连接 聚集就是求和过程
emp 表中有员工编号和工资等信息 emp_bonus表中有员工号和奖金类型信息 type 1 为奖金为员工工资*10% 2为20% 3为30% 现在要求部门为10的员工工资总和和奖金总和 先链接emp和 emp_bonus两张表 算出各员工奖金 在这个链接过程中由于有的员工有多个奖金,这个连接会产生只有奖金信息不同其他都相同的记录 在求和的时候 工资求和就会发生错误 所以要用去重
MYSQL
select deptno,sum(distinct sal) as total_sal,sum(bouns) as total_bonus from(select e.empno,e.ename,e.sal,e.deptno,e.sal*case when eb.type = 1 then .1 when eb.type =2 then .2 else .3 end as bonus from emp e,emp_bonus eb where e.empno = eb.empno and e.deptno =10)x group by deptno;
ORACLE
select distinct deptno,total_sal,total_bouns from(select e.empno,e.ename,sum(distinct e.sal)over (partition by e.deptno)as total_sal,e.deptno,sum(e.sal*case when eb.type =1 then .1 when eb.type = 2 then .2 else .3 end) over (partition by deptno)as total_bonus from emp e,emp_bonus eb where e.empno=eb.empno and e.deptno = 10) x ;
3.6 聚集与外连接 上一个问题中有的员工没有奖金 这样会影响到 工资的统计
MYSQL
select deptno,sum(distinct sal)as total_sal,sum(bouns) as total_bonus from (select e.empno,e,ename,e.sal,e.deptno,e.sal*case when eb.type is null then 0 when eb.type = 1 then .1 when eb.type = 2 then .2 else .3 end as bonus from emp e left outer join emp_bonus eb on (e.empno = eb.empno) where e.deptno =10) group by deptno;
ORACLE
select deptno,sum(distinct sal)as total_sal,sum(bouns) as total_bonus from (select e.empno,e,ename,e.sal,e.deptno,e.sal*case when eb.type is null then 0 when eb.type = 1 then .1 when eb.type = 2 then .2 else .3 end as bonus from emp e , emp_bonus eb where e.empno = eb.empno (+) and e.deptno =10) group by deptno;
3.7 替换NULL coalesce 函数 将NULL替换成想要的值
select ename,comm from emp where coalesce(comm,0)<(select comm from emp where ename = 'WARD');
SQL.Cookbook 读书笔记3 操作多个表的更多相关文章
- SQL.Cookbook 读书笔记4 插入更新和删除
第四章 插入更新和删除 4.1 插入数据 ,'PROGRA','NEW YOURK'); 4.2 从一个表向另一个表中复制 insert into dept_east(deptno,dname,loc ...
- SQL.Cookbook 读书笔记5 元数据查询
第五章 元数据查询 查询数据库本身信息 表结构 索引等 5.1 查询test库下的所有表信息 MYSQL SELECT * from information_schema.`TABLES` WHERE ...
- SQL.Cookbook 读书笔记2 查询结果排序
第二章 查询结果排序 2.1 按查询字段排序 order by sal asc; desc;-- 3表示sal 2.2 按子串查询 );--按job的最后两个字符排序 2.3 对字符数字混合排序 cr ...
- 《SQL CookBook 》笔记-第三章-多表查询
目录 3.1 叠加两个行集 3.2 合并相关行 3.3 查找两个表中相同的行 3.4 查找只存在于一个表中的数据 3.5 从一个表检索与另一个表不相关的行 3.6 新增连接查询而不影响其他连接查询 3 ...
- 《SQL CookBook 》笔记-第三章-多表查询-连接查询
目录 1 内连接(inner join) 1.1 隐式的内连接 1.2 显式的内连接 2 外连接(outer join) 2.1 左连接(left outer join) 2.2 右连接(right ...
- Linux Shell Scripting Cookbook 读书笔记 1
本系列文章为<Linux Shell Scripting Cookbook>的读书笔记,只记录了我觉得工作中有用,而我还不是很熟练的命令 书是很好的书,有许多命令由于我比较熟悉,可能就没有 ...
- 《SQL CookBook 》笔记-第一章-检索记录
目录 第一章 检索记录 1.1检索所有行和列 1.2筛选行 1.3查找满足多个查询条件的行 1.4筛选列 1.5创建列的别名 1.6 在where子句中引用别名列 1.7 串联多列的值 1.8 在se ...
- SQL SERVER读书笔记:内存
系统先操作地址空间,真正要用的时候才申请物理内存,进行使用. Reserved Memory 保留内存,虚拟内存 Commited Memory 提交内存,物理内存 [如何判断SQL SERVER ...
- SQL SERVER读书笔记:TempDB
每次SQL SERVER启动的时候,会重新创建. 用于 0.临时表 1.排序 2.连接(merge join,hash join) 3.行版本控制 临时表与表变量的区别: 1)表变量是存储在内存中的, ...
随机推荐
- jstack来分析。当linux出现cpu被java程序消耗过高时
我们使用jdk自带的jstack来分析.当linux出现cpu被java程序消耗过高时,以下过程说不定可以帮上你的忙: 1.top查找出哪个进程消耗的cpu高 21125 co_ad2 18 ...
- 使用pip安装tensorflow 0.80,python 使用tensorflow 0.80遇到的问题及处理方法
http://blog.csdn.net/levy_cui/article/details/51251095 1.python 版本切换到2.7 推荐使用pythonbrew,http://blog. ...
- php核心技术与最佳实践知识点(上)
一.基础 1.serialize:序列化一个类,只是保存了类的属性,所以还需要反序列化unserialize的时候包含该类. 2.对于将array转为object,这个转换因为没有具体的类,所以称为了 ...
- 在LoadRunner脚本中实现随机ThinkTime
一般情况下,我们都是通过Run-Time Settings来设置Think Time(思考时间),可以设置回放脚本时忽略思考时间,或者是设置回放随机的一段思考时间. By default, when ...
- Java 通过JDBC连接Mysql数据库的方法和实例
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...
- iOS开发-使用代码退出应用程序,带动画。
有时候我们需要使用代码中断程序,如果直接调用exit方法,会使得程序就像是崩溃那样,因此我们应该加上一个动画效果. 例如: AppDelegate *app = [UIApplication shar ...
- 电脑 F键(功能键)的具体作用
F1:如果你处在一个选定的程序中而需要帮助,那么请按下F1.如果现在不是处在任何程序中,而是处在资源管理器或桌面,那么按下F1就会出现Windows的帮助程序.如果你正在对某个程序进行操作,而想得到W ...
- Rational Rose2007具体安装步骤
学习了UML.那么Rational rose绘图软件当然就是不可缺少的了. 我的电脑是win7 64位的系统.以下的链接是安装软件以及破解方法.该软件是BIN格式的.也就是镜像文件.须要安装一个虚拟驱 ...
- 简体字冯|docker-安装docker私有库
原创文章,转载请注明出处. 作者:简体字丶冯; QQ:564372931 安装docker 各终端安装docker 教程 菜鸟docker教程 就挺好,本着不重复造轮子的原则就不深入了,自己学习. 如 ...
- Spring的AOP配置
Spring的AOP配置 1.先写一个普通类: package com.spring.aop; public class Common { public void execute(String us ...