MySQL syntax for count

I want to count records in a table that satisfying a condition.  But I also want to output a zero if there is no such record.  For example

Tab=
id   col1    col2    
1    a        5
2    b        0
3    c        2        
4    a       12

The query
select col1, count(*) from tab where col2 > 0 group by 1;
would return
a  2
c  1

I want the put put to also show
b   0

How do I do this?
pax

cpeters5Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ralmadaConnect With a Mentor Commented:
or

select col1, sum(if(col2 > 0,1,0))
from tab group by col1
0
 
ralmadaCommented:
select col1, sum(case when col2 > 0 then 1 else 0 end)
from tab group by col1
0
 
cpeters5Author Commented:
Perfect!
Thanks ralmada
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.