史上最全讲解:oracle数据库
基本查询语句
函数
–内置函数
–单行函数 : 一条记录返回一个结果
–组函数|多行函数|聚合函数 : 多条记录返回一个结果(对多条行记录进行处理)
当前时间 日期对象
select sysdate from dual;
–****日期对象与字符串之间的转换 to_date() to_char()
select to_char(sysdate,‘yyyy/mm/dd hh24:mi:ss’) from dual;
select to_char(sysdate,‘yyyy"年"mm"月"dd"日" hh12:mi:ss’) from dual;
–****判定函数 decode(字段,值1,结果1,值2,结果2…,默认结果) 如果以上值都不匹配,最终函数的结果为默认结果
–如果部门编号为10,添加一个伪列字段显示编号的中文名称十…
select deptno,dname,loc,decode(deptno,10,‘十’,20,‘二十’,30,‘三十’,‘haha’) 中文名称 from dept;
- 多行函数
– 统计一下一共有多少个员工
select count(empno) from emp;
select count(*) from emp;
select count(deptno) from emp;
select count(1) from emp;
select 1 from emp;
分组
– group by 分组字段1,分组字段2…
–select 数据 from 数据源 where 行过滤条件 group by 分组字段 having 组过滤条件 order by 排序字段…;
–执行流程: from – where – group by – having – select – roder by
– 找出20部门和30部门的每个最高工资
select deptno,max(sal) from emp group by deptno having deptno in(20,30); --先分组后过滤
select deptno,max(sal) from emp where deptno in (20,30) group by deptno; --先过滤后过滤
行转列
–行转列
select name,decode(course,‘语文’,score),decode(course,‘数学’,score),decode(course,‘英语’,score) from tb_student;
select name, max(decode(course, ‘语文’, score)) 语文, min(decode(course, ‘数学’, score)) 数学,min(decode(course, ‘英语’, score)) 英语 from tb_student group by name;
rowid与rownum
–rowid和rownum,相当与伪列
–rowid 相当于对象的地址,相当于表中每一条记录的地址,当数据插入到表中时候,就存在的rowid,根据地址转化的,是每一条记录的唯一
select deptno,dname,loc,rowid from dept;
select empno,ename,rowid from emp;
–去重:
–有主键的表,唯一字段,可以根据主键|唯一字段进行区分
–没有主键,没有唯一的字段,表中的数据可以存在一模一样的多条,实现去重问题,可以使用rowid
–rownum 结果集的序号,只要有一个结果集,这个结果集就存在自己的rownum,结果集中数据的编号,rownum从1开始,每次+1
select deptno,dname,rownum from dept order by deptno desc;
select empno,ename,rownum from emp order by sal desc;
–保证rownum一定永远是从1开始,每次+1有规律,顺序不会乱,可以在当前这个结果集的外面嵌套一个select,外层这个select的rownum不会乱并且有规律
select empno,ename,rownum,n from (select empno,ename,rownum n from (select empno,ename,rownum num from emp order by sal desc)) where n>=6 and n<=10; --因为:在where中使用rownum进行判断时候,rownum还不确定
select empno,ename,rownum n from (select empno,ename,rownum num from emp order by sal desc);
–分页
–oracle实现分页使用rownum ,判断rownum的值,决定每一页显示的内容
–查询20和30部门的员工,按照工资进行升序排序, 实现分页,插叙第3页 i的数据,每页显示2个 num
select empno, ename, sal
from (select empno, ename, sal, rownum n
from (select empno, ename, sal, rownum
from emp
where deptno in (20, 30)
order by sal))
where n >= (i-1)num+1
and n <= i num;
92语法与99语法
92语法
- 内连接 :有where ,满足条件才显示,不满足不显示
–92语法 select 数据 from 表1,表2,表3…;
–笛卡尔积 对乘效果
–等值连接
select empno,ename,emp.deptno,dname from emp,dept where emp.deptno = dept.deptno;
–注意:当使用同名字段时候,需要指明字段出处
select empno,ename,emp.deptno,dname from emp,dept where emp.ename = dept.dname;
–非等值连接
–查询员工信息以及每个员工的薪资等级
select empno,ename,sal,grade from emp,salgrade where sal between losal and hisal; - 自连接 一张表,当多张表使用
–查询有上级的员工的员工信息,以及这个员工的上级信息
–数据: 员工信息 上级信息
–来源: 员工表emp e1 上级表 emp e2
select * from emp e1,emp e2 where e1.mgr = e2.empno; - 外连接
–左外连接|左连接 右外连接|右连接 :主表在逗号的左边叫做左连接,主表在逗号的右边叫做右连接
–主表: 主表中的数据无论是否满足连接条件都要显示
–查询所有员工信息,以及这个员工的上级信息
–员工表作为主表
select * from emp e1,emp e2 where e1.mgr = e2.empno;
select * from emp e2,emp e1 where e1.mgr = e2.empno(+); --右连接
select * from emp e1,emp e2 where e1.mgr = e2.empno(+); --左连接
select * from emp e1,emp e2 where e1.mgr = e2.empno(+) and e1.deptno in(10,20) order by e1.empno desc; --左连接
99语法
笛卡尔积 cross join
select * from emp e,dept d;
select * from emp e cross join dept d;
- 等值连接
–自然连接natural join 自动帮你找两个表之间的同名字段|主外键关系字段左连接 自动做等值连接
–员工的信息以及所在的部门信息
select empno,deptno from emp e natural join dept d;
–注意:如果使用同名字段,不能指明出处
–join…using(同名字段名) 指定使用哪一个同名字段作为等值连接条件
select empno,deptno from emp e join dept d using(deptno);
–注意:如果使用同名字段,不能指明出处
–非等值连接
– A join B on 连接条件 join c on 连接条件 可以做等值连接 可以做非等值连接
select * from emp e join dept d on e.deptno = d.deptno where e.deptno=30;
–注意:如果使用同名字段,需要指明出处 - 内连接 (inner) join
- 外链接 left join左连接 right join右连接 全连接 full join 两边的表都作为主表
–主表 主表在join的左边,叫做左连接 left join 主表在join的右边,叫做左连接 right join ull join 两边的表都作为主表
select * from emp e1 left join emp e2 on e1.mgr=e2.empno;
select * from emp e1 right join emp e2 on e1.mgr=e2.empno;