Aysha Abdul Gafoor
Aysha Abdul Gafoor

Reputation: 11

Replace null with value in mysql query with 0

I am trying replace null with 0 with following statement but returns no recrods instead of of catid supplied and 0.

select ifnull(count(*),0) as days, catid from mytable where Id=48 and catId=7 
group by mytable.catId;

Upvotes: 1

Views: 485

Answers (2)

Thomas Landauer
Thomas Landauer

Reputation: 8355

As far as I know, COUNT(*) does never return NULL. It returns 0 if there is no record.

Upvotes: 3

Gordon Linoff
Gordon Linoff

Reputation: 1269533

count(*) never returns NULL, so you don't need any conditional logic:

select count(*) as days, catid
from mytable
where Id = 48 and catId = 7 
group by mytable.catId;

Perhaps your issue is that the query is returning no rows. If so, you can leave out the group by. Then the query will always return one row:

select count(*) as days, catid
from mytable
where Id = 48 and catId = 7 ;

Upvotes: 1

Related Questions