SQL/PHP:显示数据库中销售量最高的3种产品 [英] SQL/PHP: Show top 3 most sold products from database
本文介绍了SQL/PHP:显示数据库中销售量最高的3种产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含以下表格的数据库:
I have a database containing the following tables:
- 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种最畅销的产品,但是我不能全力以赴.
I'd like to show the 3 most sold products on my homepage, but can't wrap my head around doing this.
我尝试过:
$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'
.谢谢
I can't seem to find out where and how to place a 'count'
in this query..
Thanks
推荐答案
尝试一下:
$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";
这篇关于SQL/PHP:显示数据库中销售量最高的3种产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文