0

我有一个mysqli的问题。当我尝试在其中使用它时,我给了我一个错误php,但是当我在某些地方执行查询时database application它工作得很好。它给了我错误

SET @rank=0; SELECT (@rank := @rank+1)

我可以在其中正确执行mysql yog它。有没有其他方法可以让它工作?

这是代码:

function get_rank($branch,$cat){
    global $connection;
    $result = array();
    $rank = 0;
    if ($statement = $connection->prepare("SET @rank=0; SELECT (@rank := @rank+1) AS rank, a.branch_code_id, SUM(b.amount), c.category FROM sales_add_h AS a INNER JOIN sales_add_i AS b ON a.id = b.sales_h_id INNER JOIN control_panel_item_create AS c ON b.item_code_id = c.id WHERE a.branch_code_id = ? AND c.category = ? GROUP BY c.category, a.branch_code_id, b.amount ORDER BY SUM(b.amount) DESC")) {        
       $statement->bind_param("is",$branch,$cat); 
       $statement->execute();
       $statement->bind_result($a,$b,$c,$d);
        while ($row = $statement->fetch()) {
            array_push($result,array($a,$b,$c,$d));
        }     
        $statement->close();
    } else  {
        printf("Errormessage: %s\n", $connection->error);
        echo " error in SQL Statement.";
    }
    return $result;
}
4

2 回答 2

1

您正在执行 2 次查询您的代码,这不适用于该 PHP 函数。但是您可以将您setselect

SELECT (@rank := @rank+1) AS rank, a.branch_code_id, SUM(b.amount), c.category 
FROM sales_add_h AS a, 
     (select @rank := 0) r 
INNER JOIN sales_add_i AS b ON a.id = b.sales_h_id 
INNER JOIN control_panel_item_create AS c ON b.item_code_id = c.id
WHERE a.branch_code_id = ? 
AND c.category = ? 
GROUP BY c.category, a.branch_code_id, b.amount 
ORDER BY SUM(b.amount) DESC
于 2013-06-05T08:11:05.370 回答
0

除了我的评论,试试这个 SQL

SELECT (@rank := @rank+1) AS rank, a.branch_code_id, SUM(b.amount), c.category 
FROM sales_add_h AS a 
INNER JOIN sales_add_i AS b ON a.id = b.sales_h_id 
INNER JOIN control_panel_item_create AS c ON b.item_code_id = c.id 
CROSS JOIN (SELECT @rank=0 ) Sub1
WHERE a.branch_code_id = ? 
AND c.category = ? 
GROUP BY c.category, a.branch_code_id, b.amount 
ORDER BY SUM(b.amount) DESC
于 2013-06-05T08:14:25.750 回答