Listing both null and not null in mysql query
Posted
by tomasz
on Stack Overflow
See other posts from Stack Overflow
or by tomasz
Published on 2009-08-13T09:05:52Z
Indexed on
2010/03/29
7:33 UTC
Read the original article
Hit count: 533
Let's say I have the table
NAME | ID | REF
foo1 | 1 | NULL
foo2 | 2 | 1234
foo2 | 3 | 567
foo1 | 4 | NULL
foo3 | 5 | 89
I'd like to count all instances of NULL and NOT NULL in one query so that I can say
NAME | null | not null
foo1 | 0 | 2
foo2 | 2 | 0
foo3 | 0 | 1
I could run these two queries
select NAME,count(*) from TABLE where REF is not null
select NAME,count(*) from TABLE where REF is null
But I'm sure there must be a simple way to do it in one mysql query.
© Stack Overflow or respective owner