Search code examples
mysqlvote

mysql most voted


ID POST_ID

1 60
2 457
3 457
4 457
5 25
6 25

how i can get a list of most voted so the result would be :

457
25
60


Solution

  • SELECT post_id, count(post_id) num_votes 
      FROM your_table 
      GROUP BY post_id 
      ORDER BY num_votes DESC
    

    will give you:

    +---------+-----------+
    | post_id | num_votes |
    +---------+-----------+
    | 457     | 3         |
    | 25      | 2         |
    | 60      | 1         |
    +---------+-----------+