现在的位置: 首页 > 综合 > 正文

oracle分页

2018年05月17日 ⁄ 综合 ⁄ 共 1486字 ⁄ 字号 评论关闭
TOP分析法:oracle 分页

行内视图
行内视图是在SQL语句中使用的一个带有别名的子查询. 
在主查询FROM 子句中的子查询就是行内视图.
行内视图不是数据库的对象,所以不需要显式的创建.


Top-N分析的语法注意事项: 
1、使用了rownum这个伪列,这个伪列将会返回行号,可以作为返回记录的序列号显示。
2、在from后面使用了子查询,这是标准的行内视图的使用。
3、在子查询中使用了order by进行排序,在前面的子查询中不需要使用。
4、在主查询中通过where条件中的rownum伪列定义过滤条件,只返回最什么的前几行数据

-- 查询员工表中 employee_id为10001 10002 10003 不能使用表中的任何字段作为查询条件
  select employee_id,first_name from employees  where rownum<4
  ---它只能查询小于的,不能查询大于的,此时里面的已经变成了,逆序的排列,然后再用<这种做就OK了。
-- 查询员工表中 employee_id为100010 10009 10008 不能使用表中的任何字段作为查询条件
select employee_id,first_name from(
	select employee_id,first_name from employees  order by employee_id asc
	)
	where rownum < 4
	
--分页 每页显示3条记录   rownum rank也是oracle中的隐藏字段。
-- 第一页  查询员工表中 employee_id为10001 10002 10003 不能使用表中的任何字段作为查询条件
select   employee_id,first_name from(
      select rownum rank,employee_id,first_name from (
          select employee_id,first_name from employees  order by employee_id asc
          )
       where  rownum <4
   ) where rank>0
   
-- 第二页  查询员工表中 employee_id为10004 10005 10006 不能使用表中的任何字段作为查询条件
   select   employee_id,first_name from(
      select rownum rank,employee_id,first_name from (
          select employee_id,first_name from employees  order by employee_id asc
          )
       where  rownum <7
   ) where rank>3

-- 第三页  查询员工表中 employee_id为10007 10008 10009 不能使用表中的任何字段作为查询条件

   select   employee_id,first_name from(
      select rownum rank,employee_id,first_name from (
          select employee_id,first_name from employees  order by employee_id asc
          )
       where  rownum <10
   ) where rank>6

-- 第四页  查询员工表中 employee_id为100010 不能使用表中的任何字段作为查询条件
   select   employee_id,first_name from(
      select rownum rank,employee_id,first_name from (
          select employee_id,first_name from employees  order by employee_id asc
          )
       where  rownum <13
   ) where rank>9

抱歉!评论已关闭.