簡體   English   中英

我如何根據銷售額檢索我的產品中的前 5 名?

[英]How can i retrieve the TOP 5 in my products based on sales?

有人可以教我如何檢索表格中排名前 5 位的產品嗎? 根據銷售情況,我使用的是 codeigniter,我試過這樣的方法

$conn = new mysqli("localhost", "root","","bubblebee");
if ($conn->connect_errno) { 
    printf("Connect failed: %s\n", $conn->connect_error);
    exit();
} else {
    echo 'Connection success <br>' ;
}

$total = "SELECT product_id, SUM(amount) 
            FROM order_items 
            GROUP BY product_id 
            ORDER BY SUM(Amount) DESC 
            LIMIT 3";
$statement = $conn->query($total);
if (!$statement = $conn->query($total)) {
    echo 'Query failed: ' . $conn->error;
} else {
    foreach($statement as $row){
        echo $row['product_id'] . '<br>';
    }
}

和這個

$select = array('products.name as label', 'sum(order_items.amount) as amount');
$final = $this -> db -> select($select)
       -> from('products') 
       -> join('order_items', 'order_items.product_id = products.id', 'left') 
       -> group_by('products.id') 
       -> order_by('products.id', 'DESC') 
       -> limit(5) 
       -> get() -> result_array();

我仍然不能讓它工作,但我在我的本地主機上嘗試了我的查詢,它正在工作,它正在檢索產品 ID 和我正在使用這個查詢的數量的總和

SELECT product_id, SUM(amount) FROM order_items GROUP BY product_id ORDER BY SUM(Amount) DESC LIMIT 5

嘗試

$this->db->select('product_id, SUM(amount)');
$this->db->from('order_items');
$this->db->group_by('product_id');
$this->db->order_by('SUM(amount)', 'DESC');
$this->db->limit(5);
$query = $this->db->get();

來源SELECT product_id, SUM(amount) FROM order_items GROUP BY product_id ORDER BY SUM(Amount) DESC LIMIT 5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM