MySQL Select 语句 - 两个表,根据其他表的计数对一个表进行排序
所以我为我编写的自定义帖子系统构建了一个投票系统。
我希望能够按“投票最多”、“最喜欢”等进行排序。
我有两个表。
条目:ID、标题、帖子
投票:ID、EntryID、结果
我希望能够查询每个条目的投票表并查看有多少票,然后按每个表的投票数对条目进行排序。我搞乱了连接等,似乎无法弄清楚。有什么建议吗?
So I have built a voting system for a custom post system i wrote.
I want to be able to sort by "most voted", "Most liked", etc.
I have two tables.
Entry: ID, Title, Post
Vote: ID, EntryID, Result
I want to be able to query the vote table for each entry and see how many vote's there are, and then sort the entry's by how many vote's each table had. I have messed around with joins, etc. and cannot seem to figure it out. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要按条目项进行连接和分组,然后使用 count 和 sum 等聚合来获取投票数和投票总和:
You want to make a join and group by the entry items, then use aggregates like count and sum to get the number of votes and the sum of votes: