【问题标题】:select posts with comments with highest likes/dislikes order by xxx desc选择具有最高喜欢/不喜欢的评论的帖子,按 xxx desc 排序
【发布时间】:2018-09-14 10:31:24
【问题描述】:

我有两张桌子,postscommentsposts 表列是:id, body, user_id, likes, dislikes, timecomments 表列是:id, body, post_id, user_id, likes, dislikes, time

让我们以两个帖子(A 和 B)为例。 Post-A 有 1 条评论,有 10 条喜欢和 2 条不喜欢,Post-B 有 1 条评论,有 5 条喜欢和 12 条不喜欢。

通过 API 端点 category 订购帖子时,如果请求是针对 most disliked 评论的,我如何实现以 Post-B 开头的 ORDER BY comment.likes/dislikes DESC 语句。或者如果请求是针对most liked 评论,则从 Post-A 开始。

这就是我当前的查询,它按任何评论请求的发布 cmets 数量排序,看起来像。请注意,我没有从 comments 表中选择,因为在获取帖子后会为每个帖子 ID 加载 cmets。

<?

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 端点的样子,它可以毫无问题地检索最喜欢/最不喜欢的 cmets。

<?

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 语句中选择具有最高喜欢/不喜欢的 cmets 的帖子,同时仍然排除带有 0 个喜欢/不喜欢的 cmets 的帖子。谢谢。

【问题讨论】:

  • 鉴于您构建数据的方式,任何喜欢或不喜欢都将是永久性的,因为它们与帖子和 cmets 相关联,而不是与喜欢和不喜欢的用户相关联。您如何检查某人是否已经喜欢或不喜欢帖子或评论?至于您的问题:与您的 $orderBy 类似,您可以在 switch 案例中添加 $where 条件。在某些情况下,它将是comments.likes != 0,或comments.dislikes != 0,而在其他情况下,它只是一个空字符串。

标签: php mysql sql


【解决方案1】:

您可以使用 JOINS 排除带有 cmets 0 喜欢/不喜欢的帖子 (SELECT Posts.* FROM Posts JOIN Comments ON Posts.id = Comments.post_id WHERE Comments.likes > 0 AND Comments.dislikes > 0)

【讨论】:

    【解决方案2】:

    添加了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));
    
    }
    ?>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      相关资源
      最近更新 更多