簡體   English   中英

Oracle db分頁查詢困難,錯誤是:“命令未正確結束”

[英]Oracle db pagination difficulties with query, error is : “command not properly ended”

我正在進行以下查詢以進行分頁。 我跑的時候

$s= oci_parse($conn,"select * from TBL_Name order by D_DATE desc");
$r = oci_execute($s);

然后沒有顯示錯誤。 當我寫下面的內容時:

$s= oci_parse($conn,"select * from TBL_Name order by D_DATE desc limit $start,$limit");
$r = oci_execute($s);

error is:  oci_execute(): ORA-00933: SQL command not properly ended .

這意味着問題是" limit $start,$limit " ,但我需要這個用於分頁。 LIMIT在Oracle中無效。 現在我該怎么寫這個查詢?

limit $start,$limit僅適用於MySQL,它對Oracle或其他數據庫沒有幫助(盡管@Charles在評論中指出,LIMIT with OFFSET 也在其他地方使用 )。

對於Oracle,它就像是

select * from (
select foo.*, ROWNUM rnum
  from  
( select * from TBL_Name order by D_DATE desc  ) foo
 where ROWNUM <= $end) where rnum >= $start;

ROWNUM是你的結果集生成的東西。 pseduocolumn ..所以它總是小於等於..所以我們首先使用不同的名稱生成最大限制和別名的rownums ..並使用外部查詢引用的別名。

select * from 
( select a.*, ROWNUM rnum from 
  (select * from TBL_Name order by D_DATE desc ) a 
  where ROWNUM <= $end )
where rnum  >= $start;

PHP代碼

// Parse a query containing a bind variable.
$stmt = oci_parse($conn, "    select * from  " +
                                          " ( select a.*, ROWNUM rnum from " +
                                          "   (select * from TBL_Name order by D_DATE desc ) a "+
                                          "    where ROWNUM <= :end) "+
                                          "   where rnum  >= :start) ");

// Bind the value into the parsed statement.
oci_bind_by_name($stmt, ":end", $end);
oci_bind_by_name($stmt, ":start", $start);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM