选择故事及其评论和回复,并在一个查询中检查当前用户是否喜欢

时间:2016-05-16 23:57:18

标签: php mysql pdo

我有四张桌子

  

文章:

post_id     title    Userid    description
1           test     20           TEXT
  

评论:

comment_id     post_id    Userid    content
1                1           5        TEXT
2                1           36       TEXT
3                1           6        TEXT
  

回复

reply_id     comment_id    Userid    content
1                1           4        TEXT
2                2           5        TEXT
3                2           30       TEXT
  

喜欢:

post_id     Userid
1           5

该网站开始加载数据库中的最后20个故事..现在假设userid(5)已登录,例如post_id(1)显示为喜欢并且所有帖子都显示其评论和回复。我想在一个查询中完成它,而不是在循环内嵌套循环。

  

我的代码:

$sql1 = $conn->prepare("SELECT * FROM posts ORDER BY post_id DESC LIMIT 20");
$sql1->execute();
   while($row1=$sql1->fetch(PDO::FETCH_ASSOC)){
        ....
        // <div class="post" id="<?php echo row1['post_id']; ?> </div>
         .
         .
         ....
         $sql2 = $conn->prepare("SELECT * FROM comments WHERE post_id=:post_id");
         $sql2 ->bindValue(':post_id', $row1['post_id']);
         $sql2 -> execute();
         while($row2=$sql2 -> fetch(PDO::FETCH_ASSOC)){
                ....
                // <div class="comment" id="<?php echo row2['comment_id']; ?> </div>
                .
                .
                ....
                $sql3 = $conn->prepare("SELECT * FROM replies WHERE comment_id=:comment_id");
                $sql3 -> bindValue(':comment_id', $row2['comment_id']);
                $sql3 ->execute();
                  while($row3=$sql3 ->fetch(PDP::FETCH_ASSOC)){ .... }

         }
         $sql_like=$conn->prepare("SELECT * from likes WHERE post_id = $row1['post_id'] AND user_id=$logged_user_id");
          $sql_like->execute();
          $sql_like->rowCount() > 1 ? //userlikes : //user does not like ;
   }

2 个答案:

答案 0 :(得分:1)

你的问题不是100%明确,但你可以从这样的事情开始:

$sql1 = $conn->prepare("SELECT p.*, 
                 c.comment_id, 
                 r.reply_id, 
                 COUNT(DISTINCT l.post_id) likes
             FROM (SELECT
                FROM posts 
                ORDER BY post_id DESC LIMIT 20
             ) p
             LEFT JOIN comments c 
             ON p.post_id = c.post_id
             LEFT JOIN replies r
             ON c.comment_id=r.comment_id
             LEFT JOIN likes l
             ON l.post_id = p.post_id
               AND l.user_id=$logged_user_id");
$sql1->execute();
   $last_post_id = 0;
   $last_comment_id = 0;
   while($row = $sql1->fetch(PDO::FETCH_ASSOC)){
     if ($last_post_id != row['post_id'] ) {
        echo '<div class="post" id="'.$row['post_id'].'>'.$row['post_id'].'</div>';
        echo '<div class="like">'.$row['likes'].'</div>';
        ...
        $last_post_id = row['post_id'];
        $last_comment_id = 0;
     }
     ...
     if ($last_comment_id != $row['comment_id']) {
       echo '<div class="comment" id="'.$row['comment_id'].'>'.$row['comment_id'].'</div>';
       $last_comment_id = $row['comment_id'];
     }
     ...                    .
     echo '<div class="reply" id="'.$row['reply_id'].'>'.$row['reply_id'].'</div>';

   }

答案 1 :(得分:0)

您可以使用子查询来计算喜欢的表中与登录ID匹配的登录用户的喜欢。

SELECT *,
   (SELECT COUNT(*) FROM likes l WHERE l.post_id=p.post_id AND user_id = '$logged_user_id') AS userlikes 
   FROM posts p ORDER BY p.post_id DESC LIMIT 20