3cross
3cross

Reputation: 29

How query can i use to get a best seller?

This is my database sample

id/cart_id/title/quantity
1    1    bbok1    2
2    2    book2    3
3    3    book1    2

So in this case I have book1 with a total of 4 sales and book2 with 3 sales. However if I use order by quantity it would show book2 then book1.

How do I use the query or php code to get the same item and make it book1 4 sales, book2 3 sales?

Upvotes: 2

Views: 1461

Answers (1)

Dan Grossman
Dan Grossman

Reputation: 52372

SELECT
  title,
  SUM(quantity) AS total_sales
FROM
  books
GROUP BY
  title
ORDER BY
  total_sales DESC

Upvotes: 6

Related Questions