我加入了两张桌子。查询如下所示:
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 .
它会给我所有帖子的总评论,而不是每个帖子的总评论。有谁能告诉我我错了什么?如何获得每篇文章的总评论?
答案 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";