使用左连接获取行数

时间:2017-11-07 12:43:43

标签: php sql mysqli

我有两张桌子,一张是评论,另一张是likeordislikes

评论

+----+--------+---------------+---------+
| id | userid |   usercom     | comname |
+----+--------+---------------+---------+
| 35 |      5 | check comment |      12 |
| 36 |      6 | comment test  |      12 |
| 37 |      6 | third comment |      12 |
| 38 |      5 | number four   |      12 |
| 39 |      7 | fifth         |      13 |
| 40 |      4 | 6th           |      13 |
| 41 |     18 | seven         |      13 |
+----+--------+---------------+---------+

likesordislikes

+----+-------+------+-------+
| id | vtype | uid  | comid |
+----+-------+------+-------+
|  1 |     0 |    5 |    35 |
|  2 |     1 |    6 |    35 |
|  3 |     1 |    7 |    35 |
|  4 |     0 |    8 |    36 |
|  5 |     1 |    5 |    36 |
|  6 |     1 |    9 |    35 |
|  7 |     1 |   10 |    36 |
|  8 |     1 |   11 |    36 |
|  9 |     1 |   20 |    35 |
| 10 |     0 |    9 |    35 |
| 11 |     1 |   21 |    37 |
+----+-------+------+-------+
  • 评论 userid 是会话ID(登录用户), comname 是发布评论的帖子唯一ID在用户中。
  • likesordislikes vtype 是投票类型,其中(0 =不喜欢,1 =喜欢), uid 会记录在喜欢的用户ID中或者不喜欢评论, comid 来自评论表(id)列

现在,我想在此评论的每条评论下显示喜欢或不喜欢的总数。

我正在尝试的PHP代码在这里

$query1 = "SELECT
comments.id,
comments.usercom,
COUNT(likesordislikes.id) AS count
FROM
comments
LEFT JOIN likesordislikes ON
comments.id=likesordislikes.comid
WHERE likesordislikes.vtype='1'
GROUP BY
comments.id";

$query2 = "SELECT
comments.id,
comments.usercom,
COUNT(likesordislikes.id) AS count
FROM
comments
LEFT JOIN likesordislikes ON
comments.id=likesordislikes.comid
WHERE likesordislikes.vtype='0'
GROUP BY
comments.id";

$stmt = $DB->prepare($query1);
$stmt->execute();
$likes = $stmt->fetchAll();
$tlikes = count($likes);

$stmt = $DB->prepare($query2);
$stmt->execute();
$dislikes = $stmt->fetchAll();
$tdislikes = count($dislikes);

$slt = "SELECT * FROM `comments` where `comname` = '$c_name' and `post` = '$type'";
$res = mysqli_query($con, $slt);
while($fetch = mysqli_fetch_array($res)) { 
echo $fetch['usercom']."<br />";
echo "Likes ".$tlikes."<br />";
echo "Dislikes ".$tdislikes;
}
这样,它没有显示每条评论喜欢/不喜欢的评论 以更清晰的方式,我想要这个结果

  

评论:
  检查评论
  喜欢4 - 不喜欢2

     

评论测试
  喜欢3 - 不喜欢1

     

第三评论
  喜欢1 - 不喜欢0

     第四号   喜欢 - 不喜欢

     

第五
  喜欢 - 不喜欢

     

第六
  喜欢 - 不喜欢

     

7个
  喜欢 - 不喜欢

但它对第12条的每条评论都显示Likes 4 - Dislikes 2

任何人都可以查看其中的错误吗?

2 个答案:

答案 0 :(得分:0)

您的查询会查看comname列,这意味着它会为您提供第12条的所有内容,因为您将其称为总结而不是为每个列分开。 你应该看看ids - 比如

$slt = "SELECT * FROM `comments` where `id` = '$id' and `post` = '$type'";

其中$id是评论表中的ID。 我不知道你如何根据你所展示的代码得到这个,但试一试。

答案 1 :(得分:0)

对于所有评论,你基本上得到了所有的好恶:

$query1 = "...";
$query2 = "...";
// ...
$tlikes = count($likes);
// ...
$tdislikes = count($dislikes);

打印它们以获取所有评论:

echo $fetch['usercom']."<br />";
echo "Likes ".$tlikes."<br />";
echo "Dislikes ".$tdislikes;

这就是为什么您为每条评论获得相同的值。为了修复您当前的代码,您可以为$likes$dislikes创建一个地图,如下所示:

$stmt = $DB->prepare($query1);
$stmt->execute();
$likes = array();
while($like = $stmt->fetch(PDO::FETCH_ASSOC){
  $likes[$like['id']] = $like['count'];
}

然后,将您的打印更改为:

while($fetch = mysqli_fetch_array($res)) { 
  echo $fetch['usercom']."<br />";
  echo "Likes ".(empty($likes[$fetch['id']])?0:$likes[$fetch['id']])."<br />";
  // ...
}

注意:您可能应该过滤喜欢和不喜欢的查询,就像过滤评论查询一样(因此您不会要求您使用的内容)

另一种方法是将您的三个查询更改为单个查询,这会将您的代码更改为以下内容:

$slt =
  "SELECT" .
    " c.usercom," .
    " SUM(CASE WHEN lod.vtype=1 THEN 1 ELSE 0 END) likes," .
    " SUM(CASE WHEN lod.vtype=0 THEN 1 ELSE 0 END) dislikes" .
  " FROM" .
    " comments c LEFT JOIN likesordislikes lod ON lod.comid=c.id" .
  " WHERE" .
    " c.comname = '$c_name' AND c.post = '$type'" .
  " GROUP BY" .
    " c.id"
;
$res = mysqli_query($con, $slt);
while($fetch = mysqli_fetch_array($res)) { 
  echo $fetch['usercom']."<br />";
  echo "Likes ".$fetch['likes']."<br />";
  echo "Dislikes ".$fetch['dislikes'];
}

Here you have the query如果您希望看到它正常工作

<强>更新

如果您不想显示两个零,则可以将while更改为:

while($fetch = mysqli_fetch_array($res)) { 
  echo $fetch['usercom']."<br />";
  if($fetch['likes']==='0' && $fetch['dislikes']==='0'){
    $fetch['likes'] = '';
    $fetch['dislikes'] = '';
  }
  echo "Likes ".$fetch['likes']."<br />";
  echo "Dislikes ".$fetch['dislikes'];
}