OREACLE-02
1、组函数
在数字类型数据使用AVG and SUM 函数 – select sum(sal), avg(sal), max(sal) , min(sal) from emp; ▪ MIN and MAX适用亍任何数据类型 – select min(hiredate ) ,max(hiredate) from emp; ▪ 组函数除了count(*)外,都不跳过非空值 – select count(*) from emp; – select count(comm) from emp; – select count(1) from emp; – 不计算空值 ▪ select count(distinct deptno) from emp
在分组函数中使用NVL函数 ▪ 组函数不能处理null ▪ select avg(comm) from emp; ▪ NVL函数迫使分组函数包括空值 ▪ select avg(nvl(comm,0)) from emp 使用组函数的时候 会把null值过滤掉。 会少统计一个人。所以设置为0
组合函数下having的使用:
select avg(sal),deptno from emp where sal is not null group by deptno having avg(sal) >2000 order by avg(sal);
数据分组 ▪ 出现在SELECT列表中的字段,如果出现的位置丌是在组函数中,那么必须出现 在GROUP BY子句中 ▪ select deptno,avg(sal) from emp group by deptno ▪ GROUP BY 列可以丌在SELECT列表中 ▪ select avg(sal) from emp group by deptno ▪ 丌能在 WHERE 子句中使用组函数.丌能在 WHERE 子句中限制组. 使用Having 对分组进行限制 ▪ select avg(sal) from emp group by deptno having avg(sal) > 1000;
2、Select语句的顺序
Select子句顺序 ▪ Sql语句执行过程: 1. 读取from子句中的基本表、视图的数据,[执行笛卡尔积操作]。 2. 选取满足where子句中给出的条件表达式的元组 3. 按group子句中指定列的值分组,同时提取满足Having子句中组条件表达 式的那些组 4. 按select子句中给出的列名戒列表达式求值输出 5. Order by子句对输出的目标表进行排序。
4、多表关联查询 92语法是没有on条件 99语法添加了on 避免笛卡尔积 natural join 相当于等值连接
--当两张表中不具有相同的列名的时候,会进行笛卡儿积操作,自然连接跟92语法的自连接没有任何关系 select * from emp e natural join dept d ;
5、子查询分类
分类: 单行子查询 多行子查询 单行子查询可以用: < > = != 等查询 多行子查询使用 in all some all --any,取其中任意一个 select sal from emp where sal > any(1000,1500,3000); --some,some跟any是同一个效果,只要大于其中某一个值都会成立 select sal from emp where sal > some(1000,1500,3000); --all,大于所有的值才会成立 select sal from emp where sal > all(1000,1500,3000); --需要进行某些值的等值判断的时候可以使用in和not in --in(list), select * from emp where deptno in(10,20); 重点是:
6、null的关键点
--is null,在sql的语法中,null表示一个特殊的含义,null != null,不能使用=,!=判断,需要使用is ,is not select * from emp where comm is null; --,is not null select * from emp where comm is not null; select * from emp where null is null;
7、mysql与oreacle的区别: 获取当前日期时间不同 分页查询不同 mysql中没有rowNum 但是有roleID。 rowNum使用必须嵌套查询。在设置索引的时候,如果没有主键,会用默认的rowId当作主键,只是rowId对用户是不可见的。
--限制输出,limit,mysql中用来做限制输出的,但是oracle中不是 --再oracle中,如果需要使用限制输出和分页的功能的话,必须要使用rownum, --但是rownum不能直接使用,需要嵌套使用 --4、求薪水最高的前5名雇员 select * from (select * from emp e order by e.sal desc) t1 where rownum <= 5 select * from emp e where rownum <=5 order by e.sal desc --5、求薪水最高的第6到10名雇员 select t1.*,rownum from (select * from emp e order by e.sal desc) t1 where rownum <= 10 --使用rownum的时候必须要再外层添加嵌套,此时才能将rownum作为其中的一个列,然后再进行限制输出 select * from (select t1.*, rownum rn from (select * from emp e order by e.sal desc) t1 where rownum <= 10) t where t.rn > 5 and t.rn <= 10; select * from (select t1.*, rownum rn from (select * from emp e order by e.sal desc) t1) t where t.rn > 5 and t.rn <= 10;
8、mybaties中chose when othise 的使用
select * from t_blog where 1 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "owner1" </otherwise> </choose> </select> 上述代码 可以把where 1=1 改成where标签 choose when 会默认只有一个条件输出。都不符合则会输出 otherwise
9、组函数的使用,使用count的使用建议使用数字,不要使用count(*)
count() 记录数,处理的时候会跳过空值而处理非空值
组函数,一般情况下,组函数都要和groupby组合使用 组函数一般用于选择列表或者having条件判断 常用的组函数有5个 avg() 平均值,只用于数值类型的数据 min() 最小值,适用于任何类型 max() 最大值,适用于任何类型 count() 记录数,处理的时候会跳过空值而处理非空值 count一般用来获取表中的记录条数,获取条数的时候可以使用*或者某一个具体的列 甚至可以使用纯数字来代替,但是从运行效率的角度考虑,建议使用数字或者某一个具体的列 而不要使用* sum() 求和,只适合数值类型的数据
10、decode的使用
--给不同部门的人员涨薪,10部门涨10%,20部门涨20%,30部门涨30% select ename,sal,deptno,decode(deptno,10,sal*1.1,20,sal*1.2,30,sal*1.3) from emp; select ename, sal, deptno, case deptno when 10 then sal * 1.1 when 20 then sal * 1.2 when 30 then sal * 1.3 end from emp;
12、视图 视图是个虚拟的表 基于基表生成的。 基表数据修改了,视图的数据也可以修改。其实 就是在查询的时候,又再次查询了下视图的sql。
视图可以分为只图视图,非只读视图。 只读视图只能看不能改。
oreacle中且仅有oreacle中有 物化视图。(百度)
13、事务 如果更改了数据,没有提交事务,那么只在当前会话中生效。别人是不生效的。提交了事务,就是把数据刷新到硬盘,谁都可以查询到。
14、向数据库中插入数据:时间用to_date
insert into emp values(2222,'haha','clerk',7902,to_date('2019-11-2','YYYY-MM-dd'),1000,500,10);
insert into emp values(2222,'haha','clerk',7902,to_date('2019-11-2','YYYY-MM-dd'),1000,500,10);
本文来自博客园,作者:Jerry&Ming,转载请注明原文链接:https://www.cnblogs.com/jerry-ming/articles/16115000.html