Pushpendra Kuntal Pushpendra Kuntal - 3 months ago 5
MySQL Question

How can i set default value to 0 if there is null in result in mysql query

I want to generate a query which generate ratio girls over boys.

For this i am using following query.

SELECT (c_count_girls/c_count_boys) AS ratio FROM place WHERE placeId=1;

It works fine. But whenever the values of c_count_girls=0 and c_count_boys=0

then this gives result like this :- ratio=null

but i want in this case the result should like this:- ratio=0

is any such type of solution there so i can set default ratio=0 if null is the result.

Please help me.
Thank you in advance.

Answer
SELECT COALESCE((c_count_girls/c_count_boys), 0) AS ratio
FROM place WHERE placeId=1;

More Info about COALESCE

Comments