I am making an online shopping site in PHP & My SQL where I need to fetch top records of top 5 products which are sold most. The table name is “order_details” with fields (Order_ID, Product_ID, Quantity, Price)
Can anybody help me that what should be my SQL query to fetch this record ?
You kind help will be highly appreciated.
Thanks & regards.
What field is used to tell you what’s sold the most? Using Product_ID and totalling up the quantity of each product ID used in each order?
Yes, using totalling up the price of each product id used in each order
SELECT Product_ID, SUM(Quantity) AS TotalQuantity
GROUP BY Product_ID
ORDER BY SUM(Quantity) DESC
Taken from that and tried to modify for your use. Still a noobie here…doing what I can.
good one, ryan
but hey, why go elsewhere? we have better answers here on sitepoint
I had a general idea of how to do the query but I wanted to be 100% sure. Google gives more stackoverflow threads than sitepoint. I linked the thread just incase I did it wrong so I can give the OP a starting point to redo the query.
Is that query something you’d recommend; i.e. would you do it this way? Purely asking for my learning experience since I’m trying to improve.
Edit-Noticed a typo in the query - updated it and fixed.
[quote=“RyanReese, post:6, topic:108724”]Is that query something you’d recommend; i.e. would you do it this way? Purely asking for my learning experience since I’m trying to improve.[/quote]it’s perfect
Thanks you so much Ryan for helping me out.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.