Converting null to 0 or any other value in mysql

COALESCE(colname,0)

Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.

usage :

SELECT COALESCE(colname, 0);
SELECT COALESCE(NULL,1);

how it works:

Parameter can be a table column or a static value, if table column name is given it searches for column value in database, if column value is not null it will return column value other wise it wil look for next value in paramteres and continues until it finds out a not null value

example:

SELECT COALESCE(column, 0);
if column name is null it will return 0, as 0 is next not null value in parameters

Advertisements