带有start和limit的PHP mssql查询 - 追加查询问题

时间:2012-01-04 13:19:32

标签: php sql-server

我通常用PHP / MySQL编写代码但是这个项目必须使用MSSQL。

以下逻辑适用于MySQL,但因为在MSSQL设置中,限制是一个更复杂的查询,当我想追加查询时会导致问题。我已经玩弄了查询,试图弄清楚它应该如何,但无法使它工作。请帮忙。

    //get all products for shopkeeper
    if($limit == 0) {
        $prod_qry = "SELECT id, productName FROM products WITH(NOLOCK) WHERE shopkeeper = '$this->shopkeeper'";
    }
    if($limit > 0) {
        $prod_qry = "SELECT * FROM ( SELECT id, productName, ROW_NUMBER() OVER (ORDER BY id) as row  FROM products WITH(NOLOCK)  WHERE shopkeeper='$this->shopkeeper'  ) a WHERE row > $start and row <= $limit";
    }
    if($hidden == 1) {
        $prod_qry .= " AND Hidden !='1'";
    }

    if($rating != 0) {
        $prod_qry .= " AND rating > '$rating'";
    }

*的 修改 * 我想通了,修改了以下代码:

    $prod_qry = "SELECT * FROM ( SELECT id, productName, ROW_NUMBER() OVER (ORDER BY id) as row  FROM products WITH(NOLOCK)  WHERE shopkeeper='$this->shopkeeper'" ;

    if($hidden == 1) {
        $prod_qry .= " AND Hidden !='1'";
    }

    if($rating != 0) {
        $prod_qry .= " AND rating > '$rating'";
    }       

    $prod_qry .= ") a ";    

    if($limit > 0) {
        $prod_qry .= " WHERE row > $start and row <= $limit";
    }   

1 个答案:

答案 0 :(得分:0)

我已经用答案编辑了上面的问题。