CodeIgniter Forums
Count items in category A & B (SQL question) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Count items in category A & B (SQL question) (/showthread.php?tid=57223)



Count items in category A & B (SQL question) - El Forum - 02-27-2013

[eluser]Unknown[/eluser]
I'm trying to alter an ExpressionEngine plugin to make it work for me. I think my question is pretty easy to anwer for someone who knows SQL. I have the following table:

entry_id | cat_id
5 | 3
6 | 3
7 | 3
7 | 5
8 | 5
9 | 3
9 | 5

Now I want to count all entry_ids that are in cat_id 3 and 5 (where both categories are selected). I'm using this:

Code:
$this->EE->db->select('exp_category_posts.entry_id');
$this->EE->db->join('exp_channel_titles', 'exp_category_posts.entry_id = exp_channel_titles.entry_id' );
$this->EE->db->where_in('cat_id', $cat_id);
Where $cat_id is an array of 3 & 5.

What could I do to get 2 as a result (entries 7 and 9 makes a sum of 2).

Hope someone can help. Thanks!