[英]select posts with comments with highest likes/dislikes order by xxx desc
我有两个表, posts
和comments
。 posts
表的列为: id, body, user_id, likes, dislikes, time
。 comments
表id, body, post_id, user_id, likes, dislikes, time
: id, body, post_id, user_id, likes, dislikes, time
。
让我们假设一个场景有两个帖子(A和B)。 帖子A具有1个评论,具有10个赞和2个赞,帖子B具有1条评论,具有5个赞和12个赞。
通过API端点category
对帖子进行排序时,如果请求请求的是most disliked
评论,那么我如何实现ORDER BY comment.likes/dislikes DESC
以Post-B开头的ORDER BY comment.likes/dislikes DESC
语句。 如果请求是most liked
评论,则从Post-A开始。
这就是我当前的查询(按任何评论请求的发帖数量排序)的样子。 请注意,我不是从comments
表中选择,因为在提取帖子后,将为每个帖子ID加载评论。
<?
if (isset($_GET['ordertags'])) {
//post tags e.g general/work/school etc
$orderTags = $_GET['ordertags'];
}else {
$orderTags = "alltags";
}
if (isset($_GET['orderreactions'])) {
//post reactions, e.g date time/ most post-likes/dislikes/comments & comment-likes/dislikes etc
$orderReactions = $_GET['orderreactions'];
}else {
$orderReactions = "pdt";
}
//declare vars
$orderBy = "";
//get start offset to load the first 10 results
$start = (int)$_GET['start'];
switch ($orderReactions) {
case "pdt":
$orderBy = "ORDER BY posts.posted_at";
break;
case "mlp":
$orderBy = "AND posts.likes != 0 ORDER BY posts.likes";
break;
case "mdp":
$orderBy = "AND posts.dislikes != 0 ORDER BY posts.dislikes";
break;
case "mcp":
$orderBy = "AND posts.comments != 0 ORDER BY posts.comments";
break;
case "mlc":
$orderBy = "AND posts.comments != 0 ORDER BY posts.comments";
break;
case "mdc":
$orderBy = "AND posts.comments != 0 ORDER BY posts.comments";
break;
default:
$orderBy = "ORDER BY posts.posted_at";
break;
}
//posts from users
if ($orderTags == "alltags") {
$followingposts = $db->query('SELECT posts.id, posts.body, posts.posted_at, posts.likes, posts.dislikes, posts.tags, users.`username`, users.`profileimg` FROM users, posts
WHERE users.id = posts.user_id
'.$orderBy.' DESC LIMIT 10 OFFSET '.$start.';');
}else {
$followingposts = $db->query('SELECT posts.id, posts.body, posts.posted_at, posts.likes, posts.dislikes, posts.tags, users.`username`, users.`profileimg` FROM users, posts
WHERE users.id = posts.user_id
AND tags = :tag
'.$orderBy.' DESC LIMIT 10 OFFSET '.$start.';', array(':tag'=>$orderTags));
}
?>
这就是我的comments
终结点,它看起来像没有问题一样,它可以检索最喜欢/不喜欢的注释。
<?
if (isset($_GET['action'])) {
//comment action request i.e most liked/disliked/regular i.e postDate
$action = $_GET['action'];
}else {
$action = "reg";
}
//declare vars
$orderBy = "";
switch ($action) {
case "mdc":
$orderBy = "ORDER BY comments.dislikes DESC";
break;
case "mlc":
$orderBy = "ORDER BY comments.likes DESC";
break;
case "reg":
$orderBy = "ORDER BY comments.posted_at ASC";
break;
default:
$orderBy = "ORDER BY comments.posted_at ASC";
break;
}
//fetch comments from db
$comments = $db->query('SELECT comments.id, comments.comment, comments.post_id, comments.posted_at, comments.likes, comments.dislikes, users.username, users.profileimg FROM comments, users WHERE comments.post_id = :postid AND comments.user_id = users.id '.$orderBy.';', array(':postid'=>$_GET['postid']));
?>
因此,问题在于如何在ORDER BY DESC
语句中选择具有最高喜欢/不喜欢评论的帖子,同时仍排除具有0个喜欢/不喜欢评论的帖子。 谢谢。
您可以使用JOINS排除评论为0赞/不喜欢的帖子(选择帖子。* FROM帖子JOIN在ON.Posts.id = Comments.post_id上添加评论,在Comments.likes> 0 AND Comments.dislikes> 0上)
添加了一个INNER JOIN
一切正常
<? 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)); } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.