Oracle数据库之第二篇
/*
多表查询 多个数据库表做连接查询
使用场景: 查询的数据来源为多个表
*/
--查询员工信息和员工的部门信息
select * from emp;
select * from dept;
--使用关联条件 过滤无效数据
select * from emp,dept where emp.deptno=dept.deptno
/*
内连接 隐式内连接 select * from A,B where A.列=B.列
显式内连接 select * from A inner join B on A.列=B.列
特点 做关联查询的两个表 必须双方条件数据完全匹配 才会提取
*/
--使用显式内连接实现
select * from emp inner join dept on emp.deptno = dept.deptno /*
外连接
左外连接 select * from A left join B on A.列=B.列 以左表为基准 左表数据全部显示 右表数据作为补充显示
如果没有数据 显示空 右外连接 select * from B right join A on A.列=B.列 以右表为基准 右表数据全部显示 左表数据作为补充显示
如果没有数据 显示空
**/
--查询部门信息和部门下的员工信息 没有员工的部门也要显示
--左外连接实现
select * from dept left join emp on dept.deptno = emp.deptno
--右外连接实现
select * from emp right join dept on dept.deptno = emp.deptno
--特定要求部门显示左边
select dept.*,emp.* from emp right join dept on dept.deptno = emp.deptno
/*
oracle数据库特有的外连接
语法:使用符号(+) 实现外连接
使用方法:根据需求 将符号放在 作为补充显示的表的列后面
select * from A,B where A.列=B.列(+)
*/
--使用oracle数据库特有外连接 跟等号左右无关
select * from emp,dept where emp.deptno(+)=dept.deptno
select * from emp,dept where dept.deptno=emp.deptno(+)
/*
自连接 自己跟自己做关联查询
自连接查询 别名必须加
select * from A A1,A A2 where A1.列=A2.列
使用场景:
关联的记录在同一个表内
*/
--查询员工的信息和员工的领导信息
select e.empno,e.ename,
m.empno mgr_no,m.ename mgr_name
from emp e,emp m where e.mgr = m.empno
--在上面基础上 再查询员工的部门信息 dept
select * from dept
select e.empno,e.ename,d.dname,
m.empno mgr_no,m.ename mgr_name
from emp e,emp m,dept d
where e.mgr = m.empno and d.deptno = e.deptno
--在上面基础之上 再查询员工的工资等级 salgrade
select * from salgrade select e.empno,
e.ename,
d.dname,
s1.grade,
m.empno mgr_no,
m.ename mgr_name
from emp e, emp m, dept d, salgrade s1 where e.mgr = m.empno
and d.deptno = e.deptno
and e.sal between s1.losal and s1.hisal
--在基础之上查询领导的工资等级
select e.empno,
e.ename,
d.dname,
s1.grade,
m.empno mgr_no,
m.ename mgr_name,
s2.grade mgr_grade
from emp e, emp m, dept d, salgrade s1,salgrade s2
where e.mgr = m.empno
and d.deptno = e.deptno
and e.sal between s1.losal and s1.hisal
and m.sal between s2.losal and s2.hisal
--根据查询的数据 一张表一张表加的时候 分析表之间的关联关系
--oracle的decode函数
select e.empno,
e.ename,
d.dname,
decode(s1.grade,1,'一级',2,'二级',3,'三级',4,'四级','五级') emp_grade,
m.empno mgr_no,
m.ename mgr_name,
s2.grade mgr_grade
from emp e, emp m, dept d, salgrade s1,salgrade s2
where e.mgr = m.empno
and d.deptno = e.deptno
and e.sal between s1.losal and s1.hisal
and m.sal between s2.losal and s2.hisal ---错误示例
select e.empno,
e.ename,
d.dname,
s1.grade,
m.empno mgr_no,
m.ename mgr_name,
s1.grade mgr_grade
from emp e, emp m, dept d, salgrade s1
where e.mgr = m.empno
and d.deptno = e.deptno
and e.sal between s1.losal and s1.hisal
and m.sal between s1.losal and s1.hisal /*
子查询 在查询语句中嵌套查询语句 语法:
单行子查询 select * from A where A.列= sql返回的唯一值
多行子查询 select * from A where A.列 in sql返回单列多个值
select * from A,(sql语句返回多行多列临时表) t
where A.列 = t.列
*/
--查询比雇员7654工资高,同时从事和7788相同工作的员工信息?
--1.查询数据 员工信息
--2.数据来源 emp表
--3.查询条件 工资>7654的工资 工作=7788的工作
select sal from emp where empno=7654 --1250
select job from emp where empno=7788 --ANALYST
--使用结果查询员工
select * from emp where sal > 1250 and job = 'ANALYST'
--使用sql语句替换查询条件
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) d_min from emp group by deptno
--2.数据来源 emp sql语句得到的临时表 dept
--3.查询条件 员工工资=部门最低工资 本部门
select e.empno,e.ename,e.sal,d_m.d_min ,dept.dname
from emp e,
(select deptno,min(sal) d_min from emp group by deptno) d_m,
dept
where e.deptno = d_m.deptno and e.sal = d_m.d_min
and e.deptno = dept.deptno
--查询每个部门最低工资的员工信息
select * from emp where sal =
(select min(sal) d_min from emp group by deptno)
select * from emp where sal = (800,950,1300) select * from emp where sal in
(select min(sal) d_min from emp group by deptno) --查询不是领导的员工信息
--1.员工信息
--2.emp
--3.不是领导
--子查询空值问题 空值判断 用is null is not null 其余判断结果为UNKNOW
select * from emp where empno not in( select mgr from emp )
--需要处理空值
select * from emp where empno not in( select mgr from emp where mgr is not null )
select * from emp where empno not in( select nvl(mgr,0) from emp )
select * from emp where empno not in( select mgr from emp where mgr >0 )
/*
子查询特殊使用
exists 存在 表达式 (sql语句)
判断结果集是否存在 如果存在 exists表达式返回true
不存在 返回false
*/
--简单示例
select * from emp where exists(select * from dept)--所有员工信息
select * from emp where exists(select * from dept where deptno=123)-- 没有记录
--查询有员工的部门信息
--1.部门信息
--2.dept
--3.部门有员工
select deptno from emp; ---得到了有员工的部门编号
--使用in的方式实现
select * from dept where deptno in (select deptno from emp)
/*
普通子查询 执行顺序是 先执行子查询得到结果用于主查询
exists表达式执行顺序更改
*/
select * from dept where
exists(select * from emp where emp.deptno = dept.deptno) /* mySql 使用limit 提取特定记录条数
oracle 使用 rownum 实现提取记录 用于分页使用
rownum 是oracle数据库查询到记录 生成的一系列的数值 (1,2,3,4)
rownum用于做大于判断 没有结果 必须使用子查询先生成rownum
rowun用于小于判断可以直接查询出结果 rowunm的执行原理 :
1: 执行sql语句;
2: 取到第一条记录,赋值rownum为1;
3: 判断rownum是否满足条件,如果不满足放弃该行,满足返回该行.(不满足条件,rownum还是从1开始进行判断)
4: 继续提取记录,继续生成rownum;
5: 循环步骤3; */
--rownum的示例
select rownum,emp.* from emp where rownum >5 --没有任何记录
select rownum,emp.* from emp where rownum <5 --前四条记录
select rownum,emp.* from emp where rownum =1 --只有一条
select rownum,emp.* from emp where rownum >1 --没有
select rownum,emp.* from emp where rownum >=1 --所有记录
--先生成rownum 再使用rownum过滤5条记录以后
select * from (select rownum r,emp.* from emp) where r>5 --查询员工表中 工资最高的前三名
--工资按照倒序排序
select * from emp order by sal desc
--加入rownum
select rownum,emp.* from emp order by sal desc
--先按照工资排序 再排序基础之上生成rownum
select rownum,t.* from (select * from emp order by sal desc)t
--判断rownum提取前三条
select rownum,t.* from (select * from emp order by sal desc)t where rownum<4
--提取6--10条记录
select * from (
select rownum r,t.* from (select * from emp order by sal desc)t) tt
where r> 5 and r <11 --查询员工表中工资大于本部门平均工资的员工信息
--1.员工信息
select deptno,avg(sal) d_a from emp group by deptno
--2.emp
--3.工资>部门平均工资 必须本部门 行列转换
Total 1980 1981 1982 1987
14 1 10 1 2 1: 尝试竖起一列
上面是用年的数值做的别名,下面是年对应的入职员工数
if年1987显示值是2
decode(hire_year,'1987',hire_count) 2: 使用聚合函数处理空值 select *
from emp e, (select deptno, avg(sal) d_a from emp group by deptno) d_avg
where e.sal > d_avg.d_a
and e.deptno = d_avg.deptno --统计每年入职的员工个数
select to_char(hiredate,'yyyy') hire_year,count(*) hire_count
from emp group by to_char(hiredate,'yyyy')
--对结果集格式处理 先竖起来一列
select decode(t.hire_year,'1987',t.hire_count) "1987" from
(select to_char(hiredate,'yyyy') hire_year,count(*) hire_count
from emp group by to_char(hiredate,'yyyy') ) t
--把空值的记录过滤掉 聚合函数忽略空值的记录
select sum(decode(t.hire_year,'1987',t.hire_count)) "1987" from
(select to_char(hiredate,'yyyy') hire_year,count(*) hire_count
from emp group by to_char(hiredate,'yyyy') ) t
--补全其余的列
select sum(decode(t.hire_year,'1980',t.hire_count)) "1980",
max(decode(t.hire_year,'1981',t.hire_count)) "1981",
min(decode(t.hire_year,'1982',t.hire_count)) "1982",
avg(decode(t.hire_year,'1987',t.hire_count)) "1987"
from
(select to_char(hiredate,'yyyy') hire_year,count(*) hire_count
from emp group by to_char(hiredate,'yyyy') ) t
--使用sum对hire_count做求和运算补上total
select sum(t.hire_count) total,
sum(decode(t.hire_year,'1980',t.hire_count)) "1980",
max(decode(t.hire_year,'1981',t.hire_count)) "1981",
min(decode(t.hire_year,'1982',t.hire_count)) "1982",
avg(decode(t.hire_year,'1987',t.hire_count)) "1987"
from
(select to_char(hiredate,'yyyy') hire_year,count(*) hire_count
from emp group by to_char(hiredate,'yyyy') ) t 补充知识点:Oracle 中的分页查询
ROWNUM:表示行号,实际上只是一个列,但是这个列是一个伪列,此列可以在每张表中出
现。
ROWID:表中每行数据指向磁盘上的物理地址。 /*
集合的运算
交集 取两个集合共同的部分 intersect A(1,2,3) B(2,3,4) A交B (2,3)
并集 取两个集合最大的部分 union A(1,2,3) B(2,3,4) A并B (1,2,3,4)
union all A并B (1,2,3,2,3,4)
差集 从一个集合去掉另外一个集合剩余的部分 minus A差B (1)
*/
--范例:工资大于1500,或者是20部门下的员工
--不使用集合实现
select * from emp where sal>1500 or deptno=20
--使用集合实现 --不包含重复记录
select * from emp where sal>1500
union
select * from emp where deptno=20
--union all
select * from emp where sal>1500
union all
select * from emp where deptno=20 --范例:工资大于1500,并且是20部门下的员工
select * from emp where sal>1500 and deptno=20
--使用集合实现
select * from emp where sal>1500
intersect
select * from emp where deptno=20 --1981年入职的普通员工(不包括经理,总裁)
--使用以前知识实现
select * from emp where '1981' = to_char(hiredate,'yyyy')
and job not in ('MANAGER','PRESIDENT') --使用集合实现
select * from emp where '1981' = to_char(hiredate,'yyyy')
minus
select * from emp where job in ('MANAGER','PRESIDENT')
/*
集合的使用场景
用于做跨表合并数据使用
合并数据规则
必须合并的列的数量一致 列的数值类型相同
*/
--查询公司下所有的员工信息
select empno buisiness_no,ename buisiness_name from emp
union
select mid,mname from manager;
--数据类型不一致不能合并
select empno buisiness_no,ename buisiness_name from emp
union
select mname,mid from manager; --领导表
create table manager(
mid number(9),
mname varchar(10)
)
insert into manager values(1,'zs');
insert into manager values(2,'lisi');
commit;
Oracle数据库之第二篇的更多相关文章
- Oracle 数据库知识汇总篇
Oracle 数据库知识汇总篇(更新中..) 1.安装部署篇 2.管理维护篇 3.数据迁移篇 4.故障处理篇 5.性能调优篇 6.SQL PL/SQL篇 7.考试认证篇 8.原理体系篇 9.架构设计篇 ...
- Oracle数据库之第一篇
1 : Oracle 简介 : 是美国ORACLE公司(甲骨文)提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器IP,端口,用户名.密码,点击:连接 (CLIENT/SERVER) ...
- Oracle总结第二篇【视图、索引、事务、用户权限、批量操作】
前言 在Oracle总结的第一篇中,我们已经总结了一些常用的SQL相关的知识点了-那么本篇主要总结关于Oralce视图.序列.事务的一些内容- 在数据库中,我们可以把各种的SQL语句分为四大类- (1 ...
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...
- oracle系列--第五篇 PLSQL连接本地的Oracle数据库
这篇blog主要是针对新手,我也是个新手:) 我们把oracle成功的安装在了我们的计算机上面,那我们如何才能将PLSQL developer连 接到本地的oracle呢? 首先,我们必须有下面步准备 ...
- oracle系列--第二篇 oracle下载
对于很多新手来说,包括我之前也是这样,知道oracle数据库,但是就是不知道在哪里下载.有时候,上到oracle官方网站上面都找不到下载的地方. 这不像apache里面那么直接,我们想下载如:tomc ...
- “MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构、登录窗口、以及主界面)
“MVC+Nhibernate+Jquery-EasyUI”信息发布系统 第二篇(数据库结构.登录窗口.以及主界面) 一.在上一篇文章中,主要说的就是把主框架搭建起来,并且Nhibernate能达到增 ...
- 第二章 Oracle数据库应用
第二章 Oracle数据库应用2.1 表空间和用户权限下管理 2.1.1 表空间 2.1.1.1 分类: 永久性表空间 临时性表空间 ...
- [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了
[译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 本文首发自:博客园 文章地址: https://www.cnblogs.com/yilezhu/p/ ...
随机推荐
- c++-纯虚函数和抽象类
纯虚函数和抽象类 C面向接口编程和C多态 函数类型语法基础 函数指针做函数参数(回调函数)思想剖析 函数指针做函数参数两种用法(正向调用.反向调用) 纯虚函数 抽象类 抽象类基本概念 抽象类在多继承中 ...
- 在命令提示符下,运行Java程序时,提示"找不到或无法加载主类"
小白:在命令提示符下,运行Java程序时,提示"找不到或无法加载主类". 大神:运行Java程序的作用是让Java解释器装载,检验并运行字节码文件(.class).因此,在运行Ja ...
- Nginx入门简介和反向代理、负载均衡、动静分离理解
场景 Nginx简介 Nginx ("engine x")是一个高性能的 HTTP 和反向代理服务器 特点是占有内存少,并发能力强,事实上 nginx 的并发能力确实在同类型的网页 ...
- Sql: Oracle paging
--书分类目录kind --涂聚文 Geovin Du create table geovindu.BookKindList ( BookKindID INT PRIMARY KEY, BookKin ...
- QForkMasterInit: system error caught. error code=0x000005af, message=VirtualAllocEx failed.(遇到还没试过)
今天在使用Redis的时候出现以下错误: QForkMasterInit: system error caught. error code=0x000005af, message=VirtualAll ...
- laravel上传至服务器上出现Whoops, looks like something went wrong.
1.在本地能够很好运行的laravel,上传至服务器就出现了这个问题“Whoops, looks like something went wrong.”: 2.第一步把config/app.php文件 ...
- 操作系统——输入输出(I/O)管理
目录 一.I/O 管理概述 1.1 I/O 控制方式 1.2 I/O 软件层次结构 二.I/O 核心子系统 2.1 I/O 调度概念 2.2高速缓存与缓冲区 2.3设备分配与回收 2.4假脱机技术(S ...
- ASP.NET MVC快速开发框架FastExecutor开发全过程感受及总结
困境 追溯到2018年5月份,是个炎热的夏天,毕业后1年7个月我提出了离职,原因是受不了原来公司过度的封装框架感觉一年多毫无进步与实施天天轰炸般的电话,偶然间出去面试了一次发现自己知识真的是比较局限, ...
- Mysql 事务及其原理
Mysql 事务及其原理 什么是事务 什么是事务?事务是作为单个逻辑工作单元执行的一系列操作,通俗易懂的说就是一组原子性的 SQL 查询.Mysql 中事务的支持在存储引擎层,MyISAM 存储引擎不 ...
- 团队项目之Scrum3
小组:BLACK PANDA 时间:2019.11.23 每天举行站立式会议 提供当天站立式会议照片一张 2 昨天已完成的工作 2 完善用户注册的 ...