简体   繁体   中英

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.

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

More Info about COALESCE

Try this:

table-name-> gender ,,,,, table-columns-> gender_id , gender_type

SQL query:

select COALESCE(MAX(gender_id) , 0)+1 as max_gender_id_plus_one_fixed from gender

Output table:

max_user_id_plus_one_fixed

1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM