2

我正在从表中检索数据,但不是所有行,而是一次需要 20 行进行分页。为此,我使用了在 Mysql 中完美工作但在Oracle中不工作的 limit 关键字。

代码:

"select "+  "C.CONTRACTOR_ID,C.CONTRACTOR_NAME,nvl(C.CONTACT_PERSON_1,'-'),nvl(C.CONTACT_PERSON_2,'-'),C.REGISTRATION_NO,CRA.DESCRIPTION REG_AUTH_NAME,"+
            "to_char(C.VALID_FROM,'dd/mm/yyyy'),to_char(C.VALID_TO,'dd/mm/yyyy'),CC.DESCRIPTION CONTRACTOR_CLASS,C.INCORP_PLACE,"+
            "IT.DESCRIPTION INCORP_TYPE,nvl(to_char(C.DATE_OF_INCORP,'dd/mm/yyyy'),'-') DATE_OF_INCORP,C.ADDRESS1,nvl(C.ADDRESS2,'-'),nvl(C.EMAIL_ID,'-'),"+
            "nvl(C.WEBSITE_URL,'-'),nvl(C.PHONE_NO,'-'),nvl(C.FAX_NO,'-'),nvl(C.MOBILE_NO,'-'),C.BANK_NAME,C.BANK_BRANCH,C.ACCOUNT_NO,C.IFSC_CODE," +
            "C.PAN_NO,nvl(C.TIN_NO,'-'),nvl(C.CST_NO,'-') "+ 
"from "+
            "CONTRACTOR C "+  
            "inner join CONTRACTOR_REG_AUTH CRA on CRA.REG_AUTH_ID=C.REG_AUTH_ID "+
            "inner join CONTRACTOR_CLASS CC on CC.CLASS_ID=C.CONTRACTOR_CLASS_ID "+
            "inner join INCORPORATION_TYPE IT on IT.INCORP_TYPE=C.INCORP_TYPE "+
"limit " + offset + ", " + noOfRecords ";

这里没有按关键字排序。我正在从不同的表中检索数据,然后一次只显示 20 行。

4

1 回答 1

6

在 Oracle 中,您可以使用特殊rownum变量。此示例的行为类似于limit FirstRow, NrOfRows

select  * 
from    ( 
        select  *
        ,       rownum as rn 
        from    YourTable
        order by
                id
        ) as SubQueryAlias
where   FirstRow <= rn 
        and rn < FirstRow + NrOfRows

AskTom的此查询的优化版本,链接自此SO 问题

select  * 
from    ( 
        select  /*+ FIRST_ROWS(n) */ 
                rownum as rn
        ,       *
        from    (
                select  *
                from    YourTable
                order by 
                        id
                ) as SubQueryAlias1
        where   rownum <= FirstRow + NrOfRows
        ) as SubQueryAlias2
where   rn >= FirstRow
于 2012-07-27T06:16:53.267 回答