简体   繁体   English

Oracle db分页查询困难,错误是:“命令未正确结束”

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

I am doing the following query for pagination. 我正在进行以下查询以进行分页。 When I run 我跑的时候

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

then no error shown. 然后没有显示错误。 When I write the following: 当我写下面的内容时:

$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 .

That means problem is with " limit $start,$limit " , But I need this for pagination. 这意味着问题是" limit $start,$limit " ,但我需要这个用于分页。 LIMIT is not valid in Oracle perhaps. LIMIT在Oracle中无效。 Now how can I write this query? 现在我该怎么写这个查询?

limit $start,$limit is for MySQL only, it does not help with Oracle or other databases (although as noted by @Charles in the comments, LIMIT with OFFSET is used elsewhere as well ). limit $start,$limit仅适用于MySQL,它对Oracle或其他数据库没有帮助(尽管@Charles在评论中指出,LIMIT with OFFSET 也在其他地方使用 )。

With Oracle, it is something like 对于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 is something genrated with your resultset as. ROWNUM是你的结果集生成的东西。 pseduocolumn .. so it can be always less than equal to.. so we first generate the rownums for maximum limit and alias using a different name.. and use the alias referring from the outer query. 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 code 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