質問

Sorry about the question title, but it was the best I could think of to describe my problem.

I have an Opencart website in which I need to get the top ten best sellers to display on the page.

I had originally thought that doing a simple query like so:

SELECT name,count(*) as count FROM 'database'.oc_order_product GROUP BY name ORDER BY count DESC;

Which works fine, however I have noticed that some of the records actually have a quantity value to them as well, which obviously would need to be taken into consideration and its at this point I am a bit stumped.

Any help would be greatly appreciated.

役に立ちましたか?

解決

You want to sum the quantities for your orders instead of counting rows.

SELECT name, sum(quantity) as total 
FROM 'database'.oc_order_product 
GROUP BY name 
ORDER BY total DESC;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top