I have following query:
Select diary_id,
(select count(*)
from `comments` as c
where c.d_id = d.diary_id) as diary_comments
From `diaries` as d
It takes very many time (near 0.119415 in my case). How I could solve this problem?
I see only one way: doing additional query for comment number for each row from my main query. But it will be something like doing queries in cycle. Something like
while ($r = mysql_fetch_array($res))
{
$comments = mysql_query("select count(*) from `comments` where d_id = ".$r['diary_id']);
}
I think it`s a bad strategy. Could you advice any another version?