1

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;

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 ;

2 Comments

query returns no rows but still I want to use group by because the catid will be provided in the query in loop some catid will have value.
@AyshaAbdulGafoor . . . The group by is unnecessary. The where clause limits the output to one category. If you always want one row, then leave out the group by.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.