0

I am using a subquery to count some sub category count values. But some times it does not have any count value. At that case my quesry update field value as null. Instead of that I need to update it as zero. How can I check whether the subquery result is null or not not inside sp?

I tried like this but it is not working.

SELECT
  if(sum(sub_category),sum(sub_category),0) 
FROM myobjectcount 
WHERE 1 
GROUP BY category_id
1
  • That's because a test against null does not return true or false, but null instead, so neither part of the if gets executed, null is returned instead.
    – Johan
    Oct 21, 2011 at 10:41

1 Answer 1

3
select ifnull(sum(sub_category),0) from myobjectcount group by category_id;

The IFNULL() control flow function is a very handy MySQL function. Tends to help avoid writing nasty CASE statements!

Also - the where 1 predicate is rather unusual. It seems superfluous to me so I have left it out.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.