带有变量BETWEEN的DB2 SQL分页

时间:2015-06-24 13:56:14

标签: php sql pagination db2 between

因此,我必须使用DB2对站点数据库进行分页,每页显示10个结果。但是,当我使用FETCH FIRST 10 ROWS ONLY时,所有页面都会显示相同的10个第一个结果。我搜索并认为我必须使用BETWEEN 0 AND 9和测距变量,但无论我做什么,我似乎无法正确获取SQL代码。我总是收到错误消息odbc_fetch_row(): supplied argument is not a valid ODBC result resource

我为偏移创建了变量:

//offset for the query
$limit = 10;
$offset = ($currentpage - 1)  * $limit;
$start = $offset + 1;
$end = min(($offset + $limit), $total);
$offsetend = $end - 1;

从数据库中获取信息:

$busca="";
    if(isset($_GET['palavra']))
    {
        $busca=($_GET['palavra']);
    }
    $SQL1 = "SELECT PALAVRA.CODPAL, PALAVRA.NMPAL, PALAVRA.NMLIBTRANS 
    FROM (SELECT ROW_NUMBER() OVER (ORDER BY PALAVRA.NMPAL ASC) AS ROWNUM
    FROM MAXADM.PALAVRA WHERE PALAVRA.NMPAL LIKE '$busca%') AS P 
    WHERE P.ROWNUM BETWEEN $offset AND $offsetend";

    $rs1=getInfo($SQL1);
    while(odbc_fetch_row($rs1))
    {
    $cod_palavra = odbc_result($rs1,"CODPAL");
    $palavra = odbc_result($rs1,"NMPAL");
    $librastrans = odbc_result($rs1,"NMLIBTRANS");
    }

我做错了什么?非常感谢你!

1 个答案:

答案 0 :(得分:2)

正如目前所写,具有相关名P的子选择只包含一列,ROWNUM

SELECT PALAVRA.CODPAL, PALAVRA.NMPAL, PALAVRA.NMLIBTRANS 
FROM (
  SELECT ROW_NUMBER() OVER (ORDER BY PALAVRA.NMPAL ASC) AS ROWNUM
    FROM MAXADM.PALAVRA WHERE PALAVRA.NMPAL LIKE '$busca%'
) AS P 
WHERE P.ROWNUM BETWEEN $offset AND $offsetend

所以你显然无法从中选择PALAVRA.CODPAL, PALAVRA.NMPAL, PALAVRA.NMLIBTRANS

尝试将查询重写为

SELECT P.CODPAL, P.NMPAL, P.NMLIBTRANS 
FROM (
  SELECT 
    PALAVRA.CODPAL, PALAVRA.NMPAL, PALAVRA.NMLIBTRANS,
    ROW_NUMBER() OVER (ORDER BY PALAVRA.NMPAL ASC) AS ROWNUM
  FROM MAXADM.PALAVRA WHERE PALAVRA.NMPAL LIKE '$busca%'
) AS P 
WHERE P.ROWNUM BETWEEN $offset AND $offsetend