About MySQL ifnull functions
The MySQL IFNULL
function is one of the MySQL control flow functions that accepts two parameters, and if not NULL
, returns the first argument. Otherwise, the IFNULL
function returns a second argument.
Two parameters can be literal values or expressions.
The IFNULL
syntax of the function is described below:
IFNULL(expression_1,expression_2);
If expression_1
not NULL
, the IFNULL
function returns, expression_1
otherwise returns expression_2
the result.
IFNULL
The function returns a string or number based on the context used.
MySQL's ifnull function example
See the following IFNULL
function example:
Example-1
SELECT IFNULL(1,0); -- returns 1
Example-2
SELECT IFNULL(‘‘,1); -- returns ‘‘
Example-3
SELECT IFNULL(NULL,‘IFNULL function‘); -- returns IFNULL function
Example -4
SELECT Ifnull (Sex,'as fromuser-- If the query to this sex is a null value, then the value is assigned to an empty string
How does the statement in the above example work?
IFNULL(1,0)
Returned 1
because it is 1
not NULL
.
IFNULL(‘‘,1)
Returns ‘‘
because the ‘‘
string is not NULL
.
IFNULL(NULL,‘IFNULL function‘)
Returns a IFNULL
function string because the first argument is NULL
.
MySQL ifnull simple instructions for use