如何显示每篇文章的总评论

时间:2011-09-11 10:57:56

标签: php mysql comments

我加入了两张桌子。查询如下所示:

SELECT *, AVG(rate)
FROM comments c 
LEFT JOIN supps s     
on c.tutorialid = s.tutid 
WHERE category = 'Protein' 
GROUP BY tutorialid
ORDER BY $orderby $sort LIMIT $startrow,$limit";

我有以下代码来获得总评论:

//find the number of comments
    $commentNum = mysql_num_rows($result);

当我使用echo $commentNum时,它会显示每行所有评论的总和,例如,如果我有:

echo "".$row['title'] ."<br>";
echo $commentNum .

它会给我所有帖子的总评论,而不是每个帖子的总评论。有谁能告诉我我错了什么?如何获得每篇文章的总评论?

1 个答案:

答案 0 :(得分:2)

我会尝试在您的初始COUNT(*)中添加SELECT。所以你的查询会读到

SELECT *, COUNT(*), AVG(rate)
FROM comments c 
LEFT JOIN supps s     
ON c.tutorialid = s.tutid 
WHERE category = 'Protein' 
GROUP BY tutorialid
ORDER BY $orderby $sort LIMIT $startrow,$limit";