不管是在面试过程中,还是在功能的应用里,分页查询都是很重要的,之前是怎么也想不明白,后来静下心来,揣摩了一番,华丽的想通了!
【分页查询】
在一个页面上查看内容的时候 肯定会有 让你选择从第几页到第几页,这个功能实现是在数据库中的一挑语句就可以搞定。
比如:一般是每页5行 我要查第一页到第一页的内容。
语句为select * from (select rownum rn,a.* from (select empno,ename,sal from emp order by sal desc) a) where rn between 5*&no-5+1 and 5*&no
思路为:先 按照工资做降序排列 然后 把rownum 做别名 显示行号 然后根据别名rn 进行翻页 亮点就是在 利用rownum的别名 伪劣 做 between and 最后的运算有些绕 好好想想逻辑 最后也可以这样写效果一样 5*&no-4 and 5*&no
【行列转换】
以这个结果集为准 进行行列转换
select deptno,count(*) from emp group by deptno;
显示结果:
利用count
利用decode语句 如果(部门)deptno 是10的话 就输出一个a 执行完毕后进行count 统计有多少个a 就是有多少个10部门的。
select count(decode(deptno,10,'a')) as deptno10,
count(decode(deptno,20,'b'))as deptno20,
count(decode(deptno,30,'c'))as deptno30
from emp;
利用统计和
如果(部门)deptno 是10的话 就输出一个1 执行完毕后进行sum(求和) 总共有多少个1 就是有多少个10部门的。select sum(decode(deptno,10,1)) as deptno10,
sum(decode(deptno,20,1)) as deptno20,
sum(decode(deptno,30,1)) as deptno30
from emp;
输出结果:
转载于:https://blog.51cto.com/historys/1336148