Mysql和PHP试图根据销售商品的质量获得商品的等级

时间:2017-12-31 15:15:04

标签: php mysql

我正试图从物品已售出的总数量中获得排名到目前为止我可以获得物品已售出的总数量。但我不知道如何获得排名第1的商品排名最高的商品排名,有人可以帮我解释下面是我到目前为止得到的每件商品总量的工作代码。以下是我的PHP& MySQL代码。

PHP& MySQL代码

function rank(){
    $query = "SELECT SUM(`orders`.`quantity`) AS `total`, `orders`.`product_id`, `data`.`member_id`
              FROM `orders`
              INNER JOIN `data` ON `orders`.`product_id` = `data`.`product_id`
              GROUP BY `orders`.`product_id`
              ORDER BY `total` DESC";
    //short hand if statement ? if-true : if-false to determine the number of results returned by the query                                          
    $count = ($query = mysqli_query(database(), $query)) ? mysqli_num_rows($query) : 0; 

    if($count === 0){
        return false;       
    } else {
        while($row = mysqli_fetch_assoc($query)){
            $results[] = array(
                         'total' => $row['total'],
                         'member_id' => $row['member_id'],
                         'product_id' => $row['product_id']
            );
        }

        return $results;
    }   
}

0 个答案:

没有答案