简体   繁体   English

具有开始和限制的PHP mssql查询-追加到查询问题

[英]PHP mssql query with start and limit - append to query issue

I usually code in PHP / MySQL but for this project have to use MSSQL. 我通常使用PHP / MySQL进行编码,但是对于此项目,必须使用MSSQL。

The below logic would work in MySQL but because in MSSQL setting the limit is a much more complex query, its causing issues when I want to append to the query. 下面的逻辑在MySQL中可以使用,但是因为在MSSQL中设置限制是一个非常复杂的查询,所以当我想追加到查询时会引起问题。 I have played around with the query to try and figure out how it should be but cannot get it working. 我一直在处理该查询,以试图弄清楚应该如何,但无法使其正常工作。 Please help. 请帮忙。

    //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'";
    }

* EDIT * Ive figured it out, amended code below: * 编辑 *我已经弄清楚了,下面修改了代码:

    $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";
    }   

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM