Sanjay Nakate
Sanjay Nakate

Reputation: 2088

Select sum of column and display top 3 MAX Sum with limit LIMIT

I have bellow snippet table

table

Here i have to sum vote top 3 values.

Suppose product_id 3030 have sum of vote column is 8.1 and the 3671 sum is 5.2

here i have to get top 3 product_id which have max sum like bellow example output.

here max sum product_id id 3030 have sum 8.1 and second is 3671 have sum 5.2

I Have tried bellow query but it not showing top 3 product_id Max Top 3 of sum vote.

$query = mysql_query("SELECT sum(vote) AS 'meta_sum',product_id FROM rating GROUP BY product_id ASC LIMIT 3") OR DIE(mysql_error())

Upvotes: 0

Views: 81

Answers (1)

Giorgos Betsos
Giorgos Betsos

Reputation: 72165

Try this:

SELECT SUM(vote) AS 'meta_sum', product_id 
FROM rating 
GROUP BY product_id 
ORDER BY 'meta_sum' DESC LIMIT 3

The above query will return the top 3 products having the greatest SUM(vote) values.

Upvotes: 3

Related Questions