我的问题是similar to this one,我试过解决方案,但它不完全适合我的情况。使用SQL和JOIN的理货投票(这是可能的吗?)
我有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查询看起来像达到这个目标?
这很好,谢谢! (会显示post_title而不是id更难吗?) –
@Ankur只是加入职位并添加post_title到SELECT和GROUP BY子句 –
@Conrad Frix谢谢! –