Number of comments
Posted: Sun Feb 18, 2007 11:13 am
Hi all,
I have the following problem:
- there's one table with news,
- and a second table with comments (related to the news table by 'news_id' column)
Now, I would like to know, whether there's some possibility to get a list of news with a number of comments for each, just through one query?
I've tried this:
This does well what I need but just if there are already some comments present (if not, no row is returned because condition result is FALSE).
So, is there some other way how to do this just in one query or do I have to go through every row of the result and do a query for each?
Thanks
I have the following problem:
- there's one table with news,
- and a second table with comments (related to the news table by 'news_id' column)
Now, I would like to know, whether there's some possibility to get a list of news with a number of comments for each, just through one query?
I've tried this:
Code: Select all
SELECT *, COUNT(*) as comments_c
FROM news, comments
WHERE comments.object_id = news.id
GROUP BY news.id
ORDER BY news.time DESC
LIMIT 0, 5So, is there some other way how to do this just in one query or do I have to go through every row of the result and do a query for each?
Thanks