how to count NULL categories in one SQL question

Posted by grzes on Stack Overflow See other posts from Stack Overflow or by grzes
Published on 2010-03-19T07:09:20Z Indexed on 2010/03/19 7:11 UTC
Read the original article Hit count: 165

Filed under:
|
|

hi,

i have a blog application were Post belongsTo Category and Category hasMany Post

Post can have a Category or not - in latter case NULL value is present in Post.category_id field.

Now i would like to have following category count with single SQL query

category|post_count
--------------
PHP | 2
JavaScript | 4
SomeOtherCat | 1
NULL | 3

The clue here is that i also want to count posts without category (NULL row above). Is it posibble with one SQL query?

© Stack Overflow or respective owner

Related posts about sql

Related posts about group-by