IFNULL function is one of the MySQL control flow functions that accepts two arguments and returns the first argument if it is not NULL. Otherwise, the IFNULL function returns the second argument.
The two arguments can be literal values or expressions.
The following illustrates the syntax of the IFNULL function:
IFNULL(expression_1,expression_2);
Example:
-
SELECT IFNULL(1,0); -- returns 1 -
SELECT IFNULL('',1); -- returns '' -
SELECT IFNULL(NULL,'IFNULL function'); -- returns IFNULL function
How it works.
-
IFNULL(1,0)returns 1 because 1 is notNULL. -
IFNULL(' ',1)returns' 'because the' 'string is notNULL. -
IFNULL(NULL,'IFNULL function')returnsIFNULL functionstring because the first argument isNULL.