COUNT() comments per post with CodeIgniter
05:23 25 Jan 2017

I have 2 models "post_model" and "comment_model".

In my Post_controller, I get a result array from post_model which has all the posts.

I'm trying to add up the comments per post, but something isn't right when I append a LIMIT clause to my query.

  • comment table has post_id.

I have tried this:

SELECT p.post_id,COUNT(c.post_id)
FROM posts p
LEFT JOIN comments c USING(post_id)
GROUP BY p.post_id

but it will not work correctly if I used LIMIT 5 for example.

php codeigniter join count parent-child