我正在尝试列出前5个销售产品的列表。
在orders_form
表中,每一行都有quantity
。
php应该计算行数和数量,以列出1到5级的产品
这是我的数据库'orders_form':
| id | product_id | quantity | single_price |
| 1 | 7 | 2 | 100 |
| 2 | 3 | 1 | 100 |
| 3 | 9 | 3 | 100 |
| 4 | 3 | 1 | 100 |
我试过:
$total = 'SELECT * FROM orders_form group by product_id ORDER BY single_price DESC';
$statement = $conn->query($total);
foreach($statement as $row) {
echo $row['product_id'] . ' - ' . $row['single_price'];
echo '<br>';
}
但它并不像我想要的那样运行。。。
这是第二次尝试,来自:
$total = "SELECT SUM(quantity.number) AS total
FROM orders_form JOIN quantity
ON orders_form.product_id = quantity.product_id
GROUP BY quantity.product_id
ORDER BY total DESC
LIMIT 3";
print_r($statement);
而且是白色屏幕
聊完所有的讨论,下面是工作代码:
$conn = new mysqli("localhost", "user","pass","pw");
if ($conn->connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
} else {
echo 'Connection success <br>' ;
}
$total = "SELECT product_id, SUM(`count`)*single_price AS total FROM `orders_form` GROUP BY product_id ORDER BY total DESC LIMIT 5";
$statement = $conn->query($total);
if (!$statement = $conn->query($total)) {
echo 'Query failed: ' . $conn->error;
} else {
foreach($statement as $row){
echo $row['product_id'] . '<br>';
}
}