我正在尝试编写一个sql函数来制作流行度算法。
我想根据帖子的“post_id”将“评论”中的评论数量添加到“帖子”中,添加到“投票”表格中使用帖子的“post_id”的投票数量,所有按“posts”表中的“date”排序(时间戳)。
我不确定如何进行这种多表排序,所以我正在寻求一些指导!
先谢谢,威尔。
答案 0 :(得分:0)
我没有测试过,但它可能有效:
SELECT `post_id`, COUNT(`post_id`) AS `count` FROM `comments` GROUP BY `post_id`
此外,您可以将其与帖子表一起加入:
SELECT * FROM `posts` LEFT JOIN (SELECT `post_id`, COUNT(`post_id`) AS `count` FROM `comments` GROUP BY `post_id`) AS `p` ON(`post_id`)
答案 1 :(得分:0)
我决定只根据“投票”计数。
SELECT posts.id, posts.date, COUNT(votes.id) AS votePerPost FROM posts,votes WHERE posts.id = votes.post_id GROUP BY posts.id ORDER BY posts.date DESC, votePerPost ASC
因为这是一个足够接近的例子。我正在寻找的。 p>