ID POST_ID
1 60
2 457
3 457
4 457
5 25
6 25
我如何获得大多数投票的名单,结果将是:
457
25个
60个
答案 0 :(得分:5)
SELECT post_id
FROM my_table
GROUP BY post_id
ORDER BY COUNT(id) DESC
答案 1 :(得分:3)
SELECT post_id, count(post_id) num_votes
FROM your_table
GROUP BY post_id
ORDER BY num_votes DESC
会给你:
+---------+-----------+
| post_id | num_votes |
+---------+-----------+
| 457 | 3 |
| 25 | 2 |
| 60 | 1 |
+---------+-----------+