abhisek
abhisek

Reputation: 952

List Top Items Based on UserVotes

I have a table (in MySQL) that stores votes. The structure is like this:

id INT(11)
post_id INT(11)
vote_value INT(11)
created DATETIME

Now, vote_value can be any integer, e.g. 12, -17 for a post. The resulting vote will be 12 + (-17) = -5 for the post. What I am trying to achieve is to sort posts by this resulting votes. Is that possible? If so, help is much appreciated.

NOTE I am able to get most voted items by

SELECT post_id, COUNT(post_id) AS Count FROM my_table GROUP BY post_id ORDER BY Count DESC LIMIT 0,5

This gives me top 5 most voted posts. i.e. it selects those posts with most number of occurrences. But I am trying to sort these by resulting votes. Hope that sounds clear.

Upvotes: 1

Views: 56

Answers (1)

Brett Walker
Brett Walker

Reputation: 3576

SELECT post_id, SUM(post_id) AS Votes FROM my_table GROUP BY post_id ORDER BY Votes DESC LIMIT 0,5

I'm not a mysql user so the sum function my be wrong.

Upvotes: 2

Related Questions