zoukankan      html  css  js  c++  java
  • Oracel 分页

    面是收集的两篇关于 Oracel 用ROWNUM实现分页的文章:
    推荐直接看第二篇,是Oracle的Tom写的,最权威
    第一篇做个参考

    第一篇:

    作者: chen_liang

    对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,而且rownum不能以任何表的名称作为前缀。

    (1) rownum 对于等于某值的查询条件 (具体解释见第二篇)
    如果希望找到学生表中第一条学生的信息,可以使用rownum=1作为条件。但是想找到学生表中第二条学生的信息,使用rownum=2结果查不到数据。因为rownum都是从1开始,但是1以上的自然数在rownum做等于判断是时认为都是false条件,所以无法查到rownum = n(n>1的自然数)。

    SQL> select rownum,id,name from student where rownum=1;

    (可以用在限制返回记录条数的地方,保证不出错,如:隐式游标)

    SQL> select rownum,id,name from student where rownum=1;
        ROWNUM ID     NAME
    ---------- ------ ---------------------------------------------------
             1 200001 张一
    SQL> select rownum,id,name from student where rownum =2;
        ROWNUM ID     NAME
    ---------- ------ ---------------------------------------------------

    (2)rownum对于大于某值的查询条件(具体解释见第二篇)

       如果想找到从第二行记录以后的记录,当使用rownum>2是查不出记录的,原因是由于rownum是一个总是从1开始的伪列,Oracle 认为rownum> n(n>1的自然数)这种条件依旧不成立,所以查不到记录

    SQL> select rownum,id,name from student where rownum >2;
    ROWNUM ID     NAME


    那如何才能找到第二行以后的记录呀。可以使用以下的子查询方法来解决。注意子查询中的rownum必须要有别名,否则还是不会查出记录来,这是因为rownum不是某个表的列,如果不起别名的话,无法知道rownum是子查询的列还是主查询的列。

    SQL>select * from(select rownum no ,id,name from student) where no>2;

            NO ID     NAME
    ---------- ------ ---------------------------------------------------
             3 200003 李三
             4 200004 赵四

    SQL> select * from(select rownum,id,name from student)where rownum>2;
        ROWNUM ID     NAME
    ---------- ------ ---------------------------------------------------

    (3)rownum对于小于某值的查询条件

    如果想找到第三条记录以前的记录,当使用rownum<3是能得到两条记录的。显然rownum对于rownum<n((n>1的自然数)的条件认为是成立的,所以可以找到记录。

    SQL> select rownum,id,name from student where rownum <3;
        ROWNUM ID     NAME
    ---------- ------ ---------------------------------------------------
            1 200001 张一
            2 200002 王二

    综上几种情况,可能有时候需要查询rownum在某区间的数据,那怎么办呀从上可以看出rownum对小于某值的查询条件是人为true的,rownum对于大于某值的查询条件直接认为是false的,但是可以间接的让它转为认为是true的。那就必须使用子查询。例如要查询rownum在第二行到第三行之间的数据,包括第二行和第三行数据,那么我们只能写以下语句,先让它返回小于等于三的记录行,然后在主查询中判断新的rownum的别名列大于等于二的记录行。但是这样的操作会在大数据集中影响速度。

    SQL> select * from (select rownum no,id,name from student where rownum<=3 ) where no >=2;
            NO ID     NAME
    ---------- ------ ---------------------------------------------------
             2 200002 王二
             3 200003 李三

    (4)rownum和排序
    Oracle中的rownum的是在取数据的时候产生的序号,所以想对指定排序的数据去指定的rowmun行数据就必须注意了。

    SQL> select rownum ,id,name from student order by name;
        ROWNUM ID     NAME
    ---------- ------ ---------------------------------------------------
             3 200003 李三
             2 200002 王二
             1 200001 张一
             4 200004 赵四

    可以看出,rownum并不是按照name列来生成的序号。系统是按照记录插入时的顺序给记录排的号,rowid也是顺序分配的。为了解决这个问题,必须使用子查询

    SQL> select rownum ,id,name from (select * from student order by name);
        ROWNUM ID     NAME
    ---------- ------ ---------------------------------------------------
             1 200003 李三
             2 200002 王二
             3 200001 张一
             4 200004 赵四

    这样就成了按name排序,并且用rownum标出正确序号(有小到大)


    笔者在工作中有一上百万条记录的表,在jsp页面中需对该表进行分页显示, 便考虑用rownum来作,下面是具体方法(每页
    显示20条):

    select * from tabname where rownum<20 order by name

    但却发现oracle却不能按自己的意愿来执行,而是先随便
    取20条记录,然后再 order by,后经咨询oracle,说rownum确实就这样,想用的话,只能用子查询 来实现先排序,后rownum,方法如下:

    select * from (select * from tabname order by name) where rownum<20,但这样一来,效率会较低很多。
    后经笔者试验,只需在order by 的字段上加主键或索引即可让oracle先按 该字段排序,然后再rownum;方法不变:
       “select * from tabname where rownum<20 order by name"

    取得某列中第N大的行

    select column_name from
    (select table_name.*,dense_rank() over (order by column desc) rank from table_name)
    where rank = &N;

    假如要返回前5条记录:
    select * from tablename where rownum<6;(或是rownum <= 5 或是rownum != 6)

    假如要返回第5-9条记录:

    select * from tablename
    where …
    and rownum<10
    minus
    select * from tablename
    where …
    and rownum<5
    order by name

    选出结果后用name排序显示结果。(先选再排序)

    注意:只能用以上符号(<、<=、!=)。

    select * from tablename where rownum != 10;返回的是前9条记录。
    不能用:>,>=,=,Between...and。由于rownum是一个总是从1开始的伪列,Oracle 认为这种条件 不成立,查不到记录.

    另外,这个方法更快:

    select * from (
    select rownum r,a from yourtable
    where rownum <= 20
    order by name )
    where r > 10
    这样取出第11-20条记录!(先选再排序再选)

    要先排序再选则须用select嵌套:内层排序外层选。
    rownum是随着结果集生成的,一旦生成,就不会变化了;同时,生成的结果是依次递加的,没有1就永远不会有2!
    rownum 是在 查询集合产生的过程中产生的伪列,并且如果where条件中存在 rownum 条件的话,则:

    1: 假如 判定条件是常量,则:
    只能 rownum = 1, <= 大于1 的自然数, = 大于1 的数是没有结果的, 大于一个数也是没有结果的
    即 当出现一个 rownum 不满足条件的时候则 查询结束   this is stop key!

    2: 当判定值不是常量的时候
    若条件是 = var , 则只有当 var 为1 的时候才满足条件,这个时候不存在 stop key ,必须进行 full scan ,对每个满足其他where条件的数据进行判定
    选出一行后才能去选rownum=2的行……

    14:45 2008-3-12 补充:
    第二篇:

    今天找到了Orcale对分页的讲述:
    http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html

    下面是可以正常工作的分页SQL


     

    其中A是最内层查询结果的别名

    下面是非常关键的一节,讲的十分清楚,推荐各位仔细看看,对了解数据库查询如何工作有帮助:

    How ROWNUM Works

    ROWNUM is a pseudocolumn (not a real column) that is available in a query. ROWNUM will be assigned the numbers 1, 2, 3, 4, ... N, where N is the number of rows in the set ROWNUM is used with. A ROWNUM value is not assigned permanently to a row (this is a common misconception). A row in a table does not have a number; you cannot ask for row 5 from a table—there is no such thing.

    !ROWNUM是在什么时候被赋予每条记录的!
    Also confusing to many people is when a ROWNUM value is actually assigned. A ROWNUM value is assigned to a row after it passes the predicate phase of the query but before the query does any sorting or aggregation. Also, a ROWNUM value is incremented only after it is assigned, which is why the following query will never return a row:

    select *
      from t
     where ROWNUM > 1;Because ROWNUM > 1 is not true for the first row, ROWNUM does not advance to 2. Hence, no ROWNUM value ever gets to be greater than 1. Consider a query with this structure:

    select ..., ROWNUM
      from t
     where <where clause>
     group by <columns>
    having <having clause>
     order by <columns>;Think of it as being processed in this order:

    1. The FROM/WHERE clause goes first.
    2. ROWNUM is assigned and incremented to each output row from the FROM/WHERE clause.
    3. SELECT is applied.
    4. GROUP BY is applied.
    5. HAVING is applied.
    6. ORDER BY is applied.

    That is why a query in the following form is almost certainly an error:

    select *
      from emp
     where ROWNUM <= 5
     order by sal desc;The intention was most likely to get the five highest-paid people—a top-N query. What the query will return is five random records (the first five the query happens to hit), sorted by salary. The procedural pseudocode for this query is as follows:

    ROWNUM = 1
    for x in
    ( select * from emp )
    loop
        exit when NOT(ROWNUM <= 5)
        OUTPUT record to temp
        ROWNUM = ROWNUM+1
    end loop
    SORT TEMPIt gets the first five records and then sorts them. A query with WHERE ROWNUM = 5 or WHERE ROWNUM > 5 doesn't make sense. This is because a ROWNUM value is assigned to a row during the predicate evaluation and gets incremented only after a row passes the WHERE clause.

    Here is the correct version of this query:

    select *
      from 
    ( select *
        from emp
       order by sal desc )
     where ROWNUM <= 5;This version will sort EMP by salary descending and then return the first five records it encounters (the top-five records). As you'll see in the top-N discussion coming up shortly, Oracle Database doesn't really sort the entire result set—it is smarter than that—but conceptually that is what takes place.

    含义解释:
    1、rownum是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,
      依此类推,这个伪字段可以用于限制查询返回的总行数。
    2、rownum不能以任何基表的名称作为前缀。

    3. 在排序的时候,因为oracle是先提取记录再排序的,而oracle的rownum是在提取记录就已经生成,它优先于排序操作,所以必须使用子查询先排序

    以下是使用方法:

    GL_CAT_SUM 是我引用的表

    //返回指定行记录

    SQL>select * from(select rownum a,t.* from GL_CAT_SUM t)where a=2 

    //按伪列排序

    SQL>select * from(select rownum a,t.* from GL_CAT_SUM t)order by a

    //返回伪列大于等于1小于等于4的记录

    SQL>select *from(select rownum a,t.* from GL_CAT_SUM t where rownum<=4)where a>=1 

    //返回最后一行记录

    SQL>select * from(select rownum a,t.* from GL_CAT_SUM t)where a=(select count(*) from GL_CAT_SUM)

    //返回最后一行减N行的记录;例如最后一行的rownum是5,N是1 下面的SQL返回的就是rownum=4的记录

    SQL> select * from(select rownum a,t.* from GL_CAT_SUM t)where a=(select count(*)-N from GL_CAT_SUM)

    以上是我在网上和际工作中整理出来的..

    (转)分页查询是web开发中用的非常多个一个技巧,实现的方法也很多,针对Oracle数据库有一个很好的方法就是通过ROWNUM 伪列来实现

    select * from (

        select rownum ro , ordersid

        from orders

        where rownum <= 100

    )

    where ro >80

    这样的查出来的数据是你要的结果集中ROWNUM大约80小于100的20行从而实现分页提高查询速度

     

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/paulin/archive/2008/04/08/2260357.aspx

  • 相关阅读:
    SP1716 GSS3
    A Simple Problem with Integers题解
    P4528 [CTSC2008]图腾 题解
    P1498 南蛮图腾 题解
    P2024 [NOI2001]食物链 题解
    Windows编程 Windows程序的生与死(中)
    Windows编程 Windows程序的生与死(上)
    C#实现在注册表中保存信息
    沿路径动画(Animation Along a Path)
    倾斜动画(SkewTransform)
  • 原文地址:https://www.cnblogs.com/huqingyu/p/1640277.html
Copyright © 2011-2022 走看看