【发布时间】:2019-01-26 03:40:06
【问题描述】:
我有一个带有 post_id 外键的 cmets 表,用于帖子表。 cmets 表有likes/dislikes 列。如何按comments table DESC 中的likes 或dislikes 的编号订购posts?
cmets 和帖子是分开加载的。 cmets API 运行良好并相应地对 cmets 进行排序(最喜欢/最不喜欢),但是在显示帖子时,我通过 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 解决了我的问题
【问题讨论】: