mysql study notes -ifnull () function and nullif () function

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)Return 1, since 1not NULL.
  • IFNULL('',1)Return '', because the ''string is not NULL.
  • IFNULL(NULL,'IFNULL function')Return IFNULLfunction strings, because the first parameter NULL.
 
IFNULL Examples of functions:
  • NULIF(1,1)Return NULL, because 1equal 1.
  • NULLIF(1,2)Return 1, this is the first argument, because 1not equal 2.
  • NULLIF('MySQL NULLIF','MySQL NULLIF')Return NULL, because the two parameters are the same string.
  • NULLIF('MySQL NULLIF','MySQL NULLIF')Returns the MySQL NULLIF, because the two strings are not equal.
  • NULLIF(1,NULL)Return 1, because 1not equal NULL. NULLIF(NULL,1)Returns the first parameter, i.e. NULL, because NULLnot equal 1.

You can NULLIFfunction to prevent division by zero , as follows:

SELECT 1/NULLIF(0,0); -- return NULL 
Because 0 equal 0 , so the NULLIF(0,0) expression returns NULL . The results statement returns NULL。
 
Excerpt: https: //www.yiibai.com/mysql/ifnull.html

Guess you like

Origin www.cnblogs.com/shishibuwan/p/11208504.html