一尘不染

SQL / PHP:显示数据库中销售量最高的3种产品

sql

我有一个包含以下表的数据库:

 - product (product_id / name / price / image_id / description)
 - order ( order_id / date / status / email )
 - order_count ( order_id / product_id / number )
 - image (image_id / image_link )

我想在首页上显示3个最畅销的产品,但不能全力以赴。

我尝试了这个:

$sql = "SELECT" * 
FROM 'product' INNER JOIN 'afbeelding' 
WHERE 'product'.'image_id' = 'afbeelding'.'image_id'
GROUP BY 'product_id'
ORDER BY 'product_id' DESC
LIMIT 3;";

我似乎无法找出'count'在此查询中的位置以及如何放置..谢谢


阅读 179

收藏
2021-05-16

共1个答案

一尘不染

试试这个:

$sql = "SELECT SUM(order_count.number) AS total, image.image_link AS image_link 
FROM product JOIN order_count 
ON product.product_id = order_count.product_id 
JOIN image ON product.image_id = image.image_id
GROUP BY order_count.product_id 
ORDER BY total DESC 
LIMIT 3";
2021-05-16