how to display each article total comments
I have two tables which I have joined. The query looks like this:
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";
And I have the code below to get the total comments:
//find the number of comments
$commentNum = mysql_num_rows($result);
When I use echo $commentNum
, it displays the total for all comments on every row for example if I had:
echo "".$row['title'] ."<br>";
echo $commentNum .
It would give me the total comments for all th开发者_运维百科e posts rather the total for each individual post. Can anyone tell me what I have got wrong? How can I get total comment for each post?
I would try adding COUNT(*)
in your initial SELECT
. So your query would read
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";
精彩评论