我有一个带有post_id
外键的帖子表的评论表。注释表具有likes/dislikes
列。如何按posts
中likes
或dislikes
的数量对comments table DESC
进行排序?
评论和帖子分开加载。评论API可以很好地工作并相应地对评论进行排序(最喜欢/不喜欢),但是在显示帖子时,我通过posts.comments DESC
对其进行排序,因为我对此没有解决方法
<?
switch ($orderReactions) {
case "pdt":
if ($orderTags == "alltags") {
$orderBy = "WHERE users.id = posts.user_id ORDER BY posts.posted_at";
}else {
$orderBy = "WHERE users.id = posts.user_id AND tags=:tag ORDER BY posts.posted_at";
}
break;
case "mlp":
if ($orderTags == "alltags") {
$orderBy = "WHERE users.id = posts.user_id AND posts.likes != 0 ORDER BY posts.likes";
}else {
$orderBy = "WHERE users.id = posts.user_id AND tags=:tag AND posts.likes != 0 ORDER BY posts.likes";
}
break;
case "mdp":
if ($orderTags == "alltags") {
$orderBy = "WHERE users.id = posts.user_id AND posts.dislikes != 0 ORDER BY posts.dislikes";
}else {
$orderBy = "WHERE users.id = posts.user_id AND tags=:tag AND posts.dislikes != 0 ORDER BY posts.dislikes";
}
break;
case "mcp":
if ($orderTags == "alltags") {
$orderBy = "WHERE users.id = posts.user_id AND posts.comments != 0 ORDER BY posts.comments";
}else {
$orderBy = "WHERE users.id = posts.user_id AND tags=:tag AND posts.comments != 0 ORDER BY posts.comments";
}
break;
case "mlc":
if ($orderTags == "alltags") {
$orderBy = "INNER JOIN comments ON comments.post_id = posts.id WHERE posts.user_id = users.id AND posts.comments != 0 AND comments.likes != 0 GROUP BY post_id ORDER BY comments.likes";
}else {
$orderBy = "INNER JOIN comments ON comments.post_id = posts.id WHERE posts.user_id = users.id AND tags=:tag AND posts.comments != 0 AND comments.likes != 0 GROUP BY post_id ORDER BY comments.likes";
}
break;
case "mdc":
if ($orderTags == "alltags") {
$orderBy = "INNER JOIN comments ON comments.post_id = posts.id WHERE posts.user_id = users.id AND posts.comments != 0 AND comments.dislikes != 0 GROUP BY post_id ORDER BY comments.dislikes";
}else {
$orderBy = "INNER JOIN comments ON comments.post_id = posts.id WHERE posts.user_id = users.id AND tags=:tag AND posts.comments != 0 AND comments.dislikes != 0 GROUP BY post_id ORDER BY comments.dislikes";
}
break;
default:
if ($orderTags == "alltags") {
$orderBy = "WHERE users.id = posts.user_id ORDER BY posts.posted_at";
}else {
$orderBy = "WHERE users.id = posts.user_id AND tags=:tag ORDER BY posts.posted_at";
}
break;
}
//posts from users followed by current logged in user + logged in user
if ($orderTags == "alltags") {
$followingposts = $db->query('SELECT posts.id, posts.body, posts.posted_at, posts.likes, posts.dislikes, posts.tags, posts.user_id, users.`username`, users.`profileimg` FROM users, posts
'.$orderBy.' DESC LIMIT 10 OFFSET '.$start.';');
}else {
$followingposts = $db->query('SELECT posts.id, posts.body, posts.posted_at, posts.likes, posts.dislikes, posts.tags, posts.user_id, users.`username`, users.`profileimg` FROM users, posts
'.$orderBy.' DESC LIMIT 10 OFFSET '.$start.';', array(':tag'=>$orderTags));
}
?>
更新
代码已更新,以反映我添加的INNER JOIN
已解决问题的问题
答案 0 :(得分:0)
我假设您正在编写类似Stack Overflow之类的网站,在该网站中您有多个评论(答案),每个评论都有喜欢和不喜欢的地方(上下投票)。
我认为您可以做的最好的事情就是将所有这些都收集起来并组装成一个存储过程。从那里,您可以传入一个确定顺序的参数。至于您遇到的实际问题,您可以在comment表中对posts表进行内部联接,然后您就可以简单地按comments.likes和comments.dislikes的顺序进行排序。
以下是用于mssql服务器的正确语法,但是我确信可以轻松地将其转换为mysql。
CREATE PROCEDURE spGetPostsByNumberOfComments @OrderByType VARCHAR(50)
AS
BEGIN
SELECT *
FROM Posts
INNER JOIN Comments ON Comments.PostId = Posts.PostId
ORDER BY
CASE WHEN @OrderByType = 'CommentLikes' THEN Comments.Likes
WHEN @OrderByType = 'CommentDislikes' THEN Comments.dislikes
WHEN @OrderByType= 'PostLikes' THEN Posts.likes
WHEN @OrderByType= 'PostDislikes' THEN Posts.dislikes
ELSE Posts.PostId
END DESC
END;