Welcome Guest, Not a member yet? Register   Sign In
SQL Count Query
#2

[eluser]darkhouse[/eluser]
What you're doing now is really a JOIN, but if you change it to a LEFT JOIN you'll get the ones with no posts also.

Code:
$this->db->select('COUNT(p.post_id) post_count, c.category_id, c.category_title, c.category_desc');
$this->db->join('blog_posts p', 'p.category_id = c.category_id', 'left');
$this->db->group_by('c.category_id');
$query = $this->db->get('blog_categories c');

Should work, you might get NULL for post_count on the ones with no posts, so just display 0 in your php in that case.


Messages In This Thread
SQL Count Query - by El Forum - 01-12-2009, 10:52 PM
SQL Count Query - by El Forum - 01-12-2009, 11:17 PM



Theme © iAndrew 2016 - Forum software by © MyBB