【问题标题】:Order Table by its Foreign Key's Table Column按外键的表列排序表
【发布时间】:2019-01-26 03:40:06
【问题描述】:

我有一个带有 post_id 外键的 cmets 表,用于帖子表。 cmets 表有likes/dislikes 列。如何按comments table DESC 中的likesdislikes 的编号订购posts

cmets 和帖子是分开加载的。 cmets API 运行良好并相应地对 cme​​ts 进行排序(最喜欢/最不喜欢),但是在显示帖子时,我通过 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 解决了我的问题

【问题讨论】:

    标签: php mysql sql


    【解决方案1】:

    我假设您正在编写一个类似于 Stack Overflow 的网站,其中您有多个 cmets(答案)并且每个都可以有喜欢和不喜欢(上下投票)。

    我认为你能做的最好的事情就是把所有这些都组装成一个存储过程。从那里你可以传入一个参数来确定排序。至于您遇到的实际问题,您可以从 cmets 表对帖子表进行内部连接,然后您可以简单地说按 cmets.likes 和 cmets.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;
    

    【讨论】:

    • 是的,这就是我正在尝试做的,但我不太了解 SQL,你能给我一个示例语句吗?
    • @MaxNjoroge 我已经用一个你可能会使用的例子编辑了答案,请注意这是在 mssql 服务器的语法中,但我认为这将是一个微不足道的转换 ot mysql。希望这会有所帮助。
    • 感谢您的回答让我思考,更新了我的代码
    • @MaxNjoroge 乐于提供帮助。如果此答案已解决您的问题,请将其标记为已接受的答案(大绿色复选标记)。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 2020-09-24
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    相关资源
    最近更新 更多