【发布时间】:2011-12-20 09:40:51
【问题描述】:
我的问题是similar to this one,我已经尝试过解决方案,但它并不完全适合我的方案。
我有 2 个表格:投票和帖子。这是一个基本草图:
`posts`
----+------------------------------------------------------------------------+
| ID | post_title |
+----+-----------------------------------------------------------------------+
| 1 | Hello world. |
| 2 | This is a post! |
| 3 | What is the meaning of life? |
| 4 | Looking for a good time? |
+----+-----------------------------------------------------------------------
`votes`
+----+---------+
| ID | post_id |
+----+---------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
+----+---------+
问题:
我想知道每个帖子获得了多少票,并显示它们,使得票最高的帖子位于顶部。
Post ID Vote Count
+---------+-----------+
| 1 | 3 |
| 3 | 2 |
| 4 | 1 |
| 2 | 0 |
实现此目标的 SQL 查询是什么样的?
【问题讨论】: