I have been working on a blog system recently, and I ran into an issue. I have two tables, one for blog posts, and another for comments on those posts. This is the query that I am currently using for retrieving of all post information and also the number of comments:
SELECT bp.*, COUNT(bpc.id) AS post_comments
FROM blog_posts AS bp
LEFT JOIN blog_post_comments AS bpc ON bpc.post_id = bp.id
LIMIT 0, 10
Now, as I stated in the title, this only returns me data about one post, although it should return 10 posts. Why is this so?
Thanks!
Since you use COUNT, it will consider all posts as the same.
Try this to see if it works:
I’m not sure if that’s the problem. Just guessing. 🙂