oracle第二天笔记
多表查询
/*
多表查询:
笛卡尔积: 实际上是两张表的乘积,但是在实际开发中没有太大意义 格式: select * from 表1,表2
*/
select * from emp;
select * from dept; select * from emp, dept; select * from emp e1, dept d1 where e1.deptno = d1.deptno;
/*
内联接:
隐式内联接:
等值内联接: where e1.deptno = d1.deptno;
不等值内联接: where e1.deptno <> d1.deptno; #没有太大意义
自联接: 自己连接自己
显式内联接:
select * from 表1 inner join 表2 on 连接条件 inner 关键字可以省略
*/
select * from emp e1, dept d1 where e1.deptno <> d1.deptno; --查询员工编号,员工姓名,经理的编号,经理的姓名
select e1.empno,e1.ename,e1.mgr,m1.ename
from emp e1, emp m1 where e1.mgr= m1.empno; --查询员工编号,员工姓名,员工的部门名称,经理的编号,经理的姓名
select e1.empno,e1.ename,d1.dname,e1.mgr,m1.ename
from emp e1, emp m1,dept d1 where e1.mgr= m1.empno and e1.deptno = d1.deptno; --查询员工编号,员工姓名,员工的部门名称,经理的编号,经理的姓名,经理的部门名称
select e1.empno,e1.ename,d1.dname,e1.mgr,m1.ename,d2.dname
from emp e1, emp m1,dept d1,dept d2
where
e1.mgr= m1.empno
and e1.deptno = d1.deptno
and m1.deptno = d2.deptno
; --查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称
select e1.empno,e1.ename,d1.dname,s1.grade,e1.mgr,m1.ename,d2.dname
from emp e1, emp m1,dept d1,dept d2,salgrade s1
where
e1.mgr= m1.empno
and e1.deptno = d1.deptno
and m1.deptno = d2.deptno
and e1.sal between s1.losal and s1.hisal
; --查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称,经理的工资等级
select e1.empno,e1.ename,d1.dname,s1.grade,e1.mgr,m1.ename,d2.dname,s2.grade
from emp e1, emp m1,dept d1,dept d2,salgrade s1,salgrade s2
where
e1.mgr= m1.empno
and e1.deptno = d1.deptno
and m1.deptno = d2.deptno
and e1.sal between s1.losal and s1.hisal
and m1.sal between s2.losal and s2.hisal
; --查询员工编号,员工姓名,员工的部门名称,员工的工资等级,经理的编号,经理的姓名,经理的部门名称,经理的工资等级
--将工资等级 1,2,3,4 显示成 中文的 一级 二级 三级... select e1.empno,
e1.ename,
d1.dname,
case s1.grade
when 1 then '一级'
when 2 then '二级'
when 3 then '三级'
when 4 then '四级'
else
'五级'
end "等级",
e1.mgr,
m1.ename,
d2.dname,
decode(s2.grade,1,'一级',2,'二级',3,'三级',4,'四级','五级') "等级"
from emp e1, emp m1,dept d1,dept d2,salgrade s1,salgrade s2
where
e1.mgr= m1.empno
and e1.deptno = d1.deptno
and m1.deptno = d2.deptno
and e1.sal between s1.losal and s1.hisal
and m1.sal between s2.losal and s2.hisal
; --查询员工姓名和员工部门所处的位置
select e1.ename,d1.loc from emp e1,dept d1 where e1.deptno = d1.deptno; select * from emp e1 inner join dept d1 on e1.deptno = d1.deptno; /*
外连接: (标准,通用写法)
左外连接: left outer join 左表中所有的记录,如果右表没有对应记录,就显示空
右外连接: right outer join 右表中的所有记录,如果左表没有对应记录,就显示空
outer 关键字可以省略 Oracle中的外连接: (+) 实际上是如果没有对应的记录就加上空值
select * from emp e1,dept d1 where e1.deptno = d1.deptno(+);
*/
select * from emp e1 left outer join dept d1 on e1.deptno = d1.deptno;
insert into emp(empno,ename) values(9527,'HUAAN');
select * from emp e1,dept d1 where e1.deptno = d1.deptno(+); select * from emp e1 right outer join dept d1 on e1.deptno = d1.deptno;
select * from emp e1,dept d1 where e1.deptno(+) = d1.deptno;
/*
多表查询:
内联接
等值联接
inner join
外连接
left outer join
right outer join
Oracle特有的写法(+) 子查询: Oracle中实际没有左(右)外连接这一说法,没有 “左右”
*/
--内联接的结果
select * from emp e1,emp t1 where e1.mgr = t1.empno;
--t1表里面的所有记录都会显示出来, 如果t1.empno 在e1.mgr中没有对应的记录,就加空值
select * from emp e1,emp t1 where e1.mgr(+) = t1.empno;
--查询不是领导的员工编号
select empno from emp where empno not in (select mgr from emp where mgr is not null); --获取员工的名字和部门的名字
select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno;
----使用子查询的方式来完成
select ename,deptno from emp; select ename,deptno,deptno from emp;
--关联子查询, 子查询依赖外部查询的条件
select e.ename,e.deptno,(select d.dname from dept d where d.deptno = e.deptno ) aa from emp e; --统计薪资大于薪资最高的员工所在部门的平均工资和薪资最低的员工所在部门的平均工资的平均工资的员工信息。
子查询
/*
子查询: 查询语句中嵌套查询语句; 用来解决复杂的查询语句
查询最高工资的员工信息
单行子查询: > >= = < <= <> != 多行子查询: in not in >any >all exists not exists 查询领导信息
*/
--查询最高工资的员工信息
--1.查询出最高工资 --5000
select max(sal) from emp;
--2. 工资等于最高工资
select * from emp where sal = (select max(sal) from emp); --查询出比雇员7654的工资高,同时和7788从事相同工作的员工信息
--1.雇员7654的工资 1250
select sal from emp where empno = 7654;
--2.7788从事的工作 ANALYST
select job from emp where empno = 7788;
--3.两个条件合并
select * from emp where sal > 1250 and job = 'ANALYST'; select * from emp where sal > (select sal from emp where empno = 7654) and job = (select job from emp where empno = 7788); --查询每个部门最低工资的员工信息和他所在的部门信息
--1.查询每个部门的最低工资,分组统计
select deptno,min(sal) minsal from emp group by deptno;
--2.员工工资等于他所处部门的最低工资
select *
from emp e1,
(select deptno,min(sal) minsal from emp group by deptno) t1
where e1.deptno = t1.deptno and e1.sal = t1.minsal;
--3.查询部门相关信息
select *
from emp e1,
(select deptno,min(sal) minsal from emp group by deptno) t1,
dept d1
where e1.deptno = t1.deptno and e1.sal = t1.minsal and e1.deptno = d1.deptno; /*
内联接, 单行子查询, 多行子查询 in
not in
any
all
exists 通常情况下, 数据库中不要出现null 最好的做法加上Not null
null值并不代表不占空间, char(100) null 依然占100个字符
*/
--查询领导信息
--1.查询所有经理的编号
select mgr from emp;
select distinct mgr from emp;
--2.结果
select * from emp where empno in (select mgr from emp); --查询不是领导的信息
select * from emp where empno not in (select mgr from emp);
select * from emp where empno <>all(select mgr from emp);
--正确的写法
select * from emp where empno not in (select mgr from emp where mgr is not null); --查询出比10号部门任意一个员工薪资高的员工信息 10 20 30
select * from emp where sal >any (select sal from emp where deptno = 10); --查询出比20号部门所有员工薪资高的员工信息 10 20 30
--1.20号最高工资 5000
select max(sal) from emp where deptno =20;
--2.员工信息
select * from emp where sal > (select max(sal) from emp where deptno =20); -----使用多行子查询完成上面这题
---------20号部门所有员工薪资 (800 2975 ...)
select sal from emp where deptno = 20;
---------大于集合所有的
select * from emp where sal >all(select sal from emp where deptno = 20); /*
exists(查询语句) : 存在的意思,判断一张表里面的记录是否存在与另外一张表中
当作布尔值来处理:
当查询语句有结果的时候, 就是返回true
否则返回的是false
数据量比较大的时候是非常高效的
*/
select * from emp where exists(select * from emp where deptno = 1234567);
select * from emp where 3=4; select * from emp where exists(select * from emp where deptno = 20); --查询有员工的部门的信息
select * from dept d1 where exists(select * from emp e1 where e1.deptno = d1.deptno ); --找到员工表中工资最高的前三名(降序排序)
select * from emp order by sal desc;
/*
rownum : 伪列, 系统自动生成的一列, 用来表示行号 rownum是Oracle中特有的用来表示行号的, 默认值/起始值是 1 ,在每查询出结果之后,再添加1 rownum最好不能做大于号判断,可以做小于号判断 SQL执行顺序
from .. where ..group by..having .. select..rownum..order by
*/
Select rownum,e1.* from emp e1; --查询rownum大于2的所有记录 ,
select rownum,e1.* from emp e1 where rownum > 2; --没有任何记录 --查询rownum大于等于1的所有记录
select rownum,e1.* from emp e1 where rownum >=1; --查询rownum < 6 的所有记录
select rownum,e1.* from emp e1 where rownum < 6; --rownum 排序
Select rownum,e1.* from emp e1 order by sal; --找到员工表中工资最高的前三名
select e1.* from emp e1 order by sal desc;
--将上面的结果当作一张表处理,再查询
select rownum, t1.* from (select e1.* from emp e1 order by sal desc) t1; --只要显示前三条记录
select rownum, t1.* from (select e1.* from emp e1 order by sal desc) t1 where rownum < 4; --找到员工表中薪水大于本部门平均薪水的员工
--1.分组统计部门平均薪水
select deptno,avg(sal) avgsal from emp group by deptno;
--2.员工工资 > 本部门平均工资
select * from emp e1,(select deptno,avg(sal) avgsal from emp group by deptno) t1
where e1.deptno = t1.deptno and e1.sal > t1.avgsal;
/*
关联子查询 , 非关联子查询
*/
select * from emp e where sal > (select avg(sal) from emp e2 group by deptno having e.deptno=e2.deptno); /*
统计每年入职的员工个数
*/
select hiredate from emp;
--只显示年
select to_char(hiredate,'yyyy') from emp;
--分组统计
select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy'); select yy
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; select case yy when '' then cc end
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; select case yy when '' then cc end "1987"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; --去除行记录中的空值
select sum(case yy when '' then cc end) "1987"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; --统计员工的总数
select sum(cc) "TOTAL"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; --将1987 和TOTAL 合并在一起
select
sum(cc) "TOTAL",
sum(case yy when '' then cc end) "1987"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; --显示所有年份的结果
select
sum(cc) "TOTAL",
sum(case yy when '' then cc end) "1980",
sum(case yy when '' then cc end) "1981",
sum(case yy when '' then cc end) "1982",
sum(case yy when '' then cc end) "1987"
from
(select to_char(hiredate,'yyyy') yy,count(1) cc from emp group by to_char(hiredate,'yyyy')) tt; /*
rowid : 伪列 每行记录所存放的真实物理地址
rownum : 行号 , 每查询出记录之后,就会添加一个行号
*/
select rowid,e.* from emp e; --去除表中重复记录
create table p(
name varchar2(10)
); insert into p values('黄伟福');
insert into p values('赵洪');
insert into p values('杨华'); delete from p where select rowid,p.* from p;
select distinct * from p; delete from p p1 where rowid > (select min(rowid) from p p2 where p1.name = p2.name); #具体过程/*
rownum : 分页查询
在oracle中只能使用子查询来做分页查询
*/
--查询第6 - 第10 记录
select rownum, emp.* from emp; select rownum hanghao, emp.* from emp; select * from (select rownum hanghao, emp.* from emp) tt where tt.hanghao between 6 and 10;
集合查询
/*
集合运算:
并集: 将两个查询结果进行合并
交集
差集 所有的查询结果可能不是来自同一张表,
emp 2000年
2017年 手机 详细信息 emp2017 */
--工资大于1500,或者20号部门下的员工
select * from emp where sal > 1500 or deptno = 20; --工资大于1500
select * from emp where sal > 1500;
--20号部门下的员工
select * from emp where deptno = 20; --并集运算: union union all
/*
union : 去除重复的,并且排序
union all : 不会去除重复的
*/
select * from emp where sal > 1500
union
select * from emp where deptno = 20; select * from emp where sal > 1500
union all
select * from emp where deptno = 20; /*
交集运算: intersect */
--工资大于1500,并且20号部门下的员工
select * from emp where sal > 1500;
select * from emp where deptno = 20; select * from emp where sal > 1500
intersect
select * from emp where deptno = 20; /*
差集运算: 两个结果相减
*/
--1981年入职员工(不包括总裁和经理)
--1981年入职员工
select * from emp where to_char(hiredate,'yyyy')=''; --总裁和经理
select * from emp where job = 'PRESIDENT' or job = 'MANAGER'; select * from emp where to_char(hiredate,'yyyy')=''
minus
select * from emp where job = 'PRESIDENT' or job = 'MANAGER'; /*
集合运算中的注意事项:
1.列的类型要一致
2.按照顺序写
3.列的数量要一致,如果不足,用空值填充
*/
select ename,sal from emp where sal > 1500
union
select ename,sal from emp where deptno = 20;
--列的类型不匹配
select ename,sal from emp where sal > 1500
union
select sal,ename from emp where deptno = 20; --列的数量不匹配
select ename,sal,deptno from emp where sal > 1500
union
select ename,sal from emp where deptno = 20; select ename,sal,deptno from emp where sal > 1500
union
select ename,sal,null from emp where deptno = 20; select ename,sal,deptno from emp where sal > 1500
union
select ename,sal,66 from emp where deptno = 20;
并集: 将两个查询结果进行合并
交集:
差集:
oracle第二天笔记的更多相关文章
- Oracle RAC学习笔记:基本概念及入门
Oracle RAC学习笔记:基本概念及入门 2010年04月19日 10:39 来源:书童的博客 作者:书童 编辑:晓熊 [技术开发 技术文章] oracle 10g real applica ...
- Oracle基础学习笔记
Oracle基础学习笔记 最近找到一份实习工作,有点头疼的是,有阶段性考核,这...,实际想想看,大学期间只学过数据库原理,并没有针对某一数据库管理系统而系统的学习,这正好是一个机会,于是乎用了三天时 ...
- EntityFramework CodeFirst SQLServer转Oracle踩坑笔记
接着在Oracle中使用Entity Framework 6 CodeFirst这篇博文,正在将项目从SQLServer 2012转至Oracle 11g,目前为止遇到的问题在此记录下. SQL Se ...
- python核心编程第二版笔记
python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d 提供调试输出1.2 –O 生成优化的字节码(生成 ...
- Oracle第二天
Oracle第二天 整体安排(3天) 第一天:Oracle的安装配置(服务端和客户端),SQL增强(单表查询). 第二天:SQL增强(多表查询.子查询.伪列-分页),数据库对象(表.约束.序列),Or ...
- Oracle RAC学习笔记01-集群理论
Oracle RAC学习笔记01-集群理论 1.集群相关理论概述 2.Oracle Clusterware 3.Oracle RAC 原理 写在前面: 最近一直在看张晓明的大话Oracle RAC,真 ...
- Oracle RAC学习笔记02-RAC维护工具集
Oracle RAC学习笔记02-RAC维护工具集 RAC维护工具集 1.节点层 2.网络层 3.集群层 4.应用层 本文实验环境: 10.2.0.5 Clusterware + RAC 11.2.0 ...
- C++标准库第二版笔记 3 和异常的理解 1
C++标准库第二版笔记 3 和异常的理解 1 差错和异常(error and exception)的处理 标准异常类(exception class) 定义于 分为: 1.语言本身支持的异常 2.标准 ...
- C++标准库第二版笔记 2.1
C++标准库第二版笔记 2.1 1 Range-Based for 循环 for ( decl : coll ) { statements; } // collaborate 类似C# foreach ...
随机推荐
- RHEL7安装配置VNC
RHEL7安装配置VNC 作者:Eric 微信:loveoracle11g 安装配置VNC服务程序 [root@zhouwanchun yum.repos.d]# cd ~ [root@zhouwan ...
- Go语言 数据类型,流程控制
Go语言 数据类型,流程控制 人生苦短,Let's Go ! package main // 必须要有一个main包 import "fmt" func main() { fmt. ...
- Windows配置多个git用户
Window配置多个Git账户,SSH连接GitHub.GitLab 最新版本GIt配置对应多个Git仓库(不需要添加多个用户名和邮箱): 在本地git上添加一个用户名和邮箱,生成一对公钥和私钥,把公 ...
- CRM 2016 请求"System.Security.Permissions.FilelOPermission,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"类型的权限已失败.
CRM 请求"System.Security.Permissions.FilelOPermission,mscorlib,Version=4.0.0.0,Culture=neutral,Pu ...
- sed初学者实用说明
转自:http://www.codeweblog.com/sed%E5%88%9D%E5%AD%A6%E8%80%85%E5%AE%9E%E7%94%A8%E8%AF%B4%E6%98%8E/ ...
- layui之初始化加分页重复请求问题解决
layui框架中的page困扰我很久,一个页面初始化后并且分页,导致初始化渲染请求一次,分页再请求了一次,一个接口就重复请求了2次,通过不停的分析和测试,最终解决了这个问题. 基于JQ的ajax二次封 ...
- 配置Jsp错误页面
配置Jsp错误页面一般我们有2种做法: (1)在页面中用指令进行配置,即page指令的errorPage和isErrorPage:可以使用page指令的errorPage来指定错误页!在当前JSP页面 ...
- php删除字符串最后一位
一.前言 从数据库中select()读取一对多的信息时,经常需要将取出的数组用某个特定的字符分割,然后拼接成字符串. 常见的语法格式: foreach ($arr as $key => $val ...
- json.loads()的字符串中为单引号引发的错误
如下错误属于弱智错误,但是错的原因让我无语,所以记录一下 str2="{'card':6217001650004184441}"print(json.loads(str2)) Tr ...
- mysql存储过程的编写
1.MySQL 新增存储过程,因为mysql默认以:为分隔符,该分隔符会使mysql自动执行sql语句,故需要将分隔符修改下,下面通过DELIMITER设为$$,然后编写SQL,编写完成再将:设为分隔 ...