IFNULL () function - if the first parameter is not NULL
, the first argument is returned, otherwise the second parameter. Two parameters can be a literal value or expression.
NULLIF () function - if the first parameter is equal to the second argument is returned NULL
, otherwise the first parameter.
NULLIF
Examples of functions:
IFNULL(1,0)
Return1
, since1
notNULL
.IFNULL('',1)
Return''
, because the''
string is notNULL
.IFNULL(NULL,'IFNULL function')
ReturnIFNULL
function strings, because the first parameterNULL
.
IFNULL
Examples of functions:
NULIF(1,1)
ReturnNULL
, because1
equal1
.NULLIF(1,2)
Return1
, this is the first argument, because1
not equal2
.NULLIF('MySQL NULLIF','MySQL NULLIF')
ReturnNULL
, because the two parameters are the same string.NULLIF('MySQL NULLIF','MySQL NULLIF')
Returns the MySQLNULLIF
, because the two strings are not equal.NULLIF(1,NULL)
Return1
, because1
not equalNULL
.NULLIF(NULL,1)
Returns the first parameter, i.e.NULL
, becauseNULL
not equal1
.
You can NULLIF
function to prevent division by zero , as follows:
SELECT 1/NULLIF(0,0); -- return NULL
Excerpt: https: //www.yiibai.com/mysql/ifnull.html