id category active ------------------------ 1 1 1 2 3 0 3 3 1 4 3 1 5 1 0
I want to select total count of each category
and count of active in each cateogry
.
Since there's no category=2, I used IFNULL, but it didn't return 0
SELECT IFNULL(COUNT(id), 0) AS total, SUM(CASE WHEN active = 1 THEN 1 ELSE 0 END) AS active FROM table WHERE category IN (1,2,3) GROUP BY category
The result returned 2 arrays, instead of 3 as expected
// result from category=1 Array ( [total] => 2 [active] => 1 ) // result from category=3 Array ( [total] => 3 [active] => 2 )
How can I get category=2 into the result
// result from category=2 Array ( [total] => 0 [active] => 0 )