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/ ...
随机推荐
- 不加班的秘诀:如何通过AOE快速集成NCNN?
作为我司头发储量前三的程序员 始终仗着头发多奋斗在加班的第一线 时时灵魂拷问自己 年轻人,你凭什么不加班? 虽然我没有女朋友但是,我有代码呀 但我不明白的是,隔壁工位那个,到岗比我迟,下班比我早,天天 ...
- web端百度地图API实现实时轨迹动态展现
最近在工作中遇到了一个百度地图api中的难题,恐怕有的程序员可能也遇到过.就是实时定位并显示轨迹,网上大部分都是通过创建polyline对象贴到地图上.当然,百度地图的画线就是这样实现的,但是好多人会 ...
- 拖动条(SeekBar)的功能与用法
拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程度,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...
- Java多线程面试问答
今天,我们将讨论Java 多线程面试问答. 线程是Java面试问题中的热门话题之一.在这里,我从面试的角度列出了大多数重要的Java多线程面试问题,但是您应该对Java线程有足够的知识来处理后续问题. ...
- IT兄弟连 HTML5教程 CSS3属性特效 2D变换1
通过CSS3转换,能够对元素进行移动.缩放.转动.拉长或拉伸.它如何工作?转换是使元素改变形状.尺寸和位置的一种效果.CSS3转换包括2D转换和3D转换,本小结我们来了解2D变换的转换方法. 转换属性 ...
- 自己封装Linux命令行万能解压命令
问题背景 Linux下经常需要解压文件,直接在命令行敲命令解压是最便捷的. 但问题在于,不同的压缩格式,需要用不同命令和不同参数,完全记不住啊. 解决方式 既然记不住,那就换一种思路,假如有一条命令能 ...
- React的setState学习及应用
React的setState学习及应用 一:作用: setState() 将对组件 state 的更改排入队列,并通知 React 需要使用更新后的 state 重新渲染此组件及其子组件.这是用于更新 ...
- C# FlagAttriute 的 小妙招
FlagAttriute ,指示可将枚举视为位域(即一组标志). 官网中文解说:https://docs.microsoft.com/zh-cn/dotnet/api/system.flagsattr ...
- 【转载】C#string.Formart的字符串格式化
String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项.Stri ...
- ABAP分享二 ALV标准范例DEMO汇总
SAP软件针对ALV的使用方法,提供了ALV标准demo程序: sap提供的ALV标准demo程序,只是展示简单的数据,用function ALV即可,若有复杂的增删改查操作 建议使用OO ALV,下 ...