mysql IFNULL function and COALESCE function tips

 IFNULL () function

 
   The IFNULL () function for determining whether the first expression is NULL, if the second parameter is NULL value is returned, if not the first parameter is NULL return value.

IFNULL () function syntax is:

IFNULL(expression, alt_value)

 

      If the first argument expression expression is NULL, the second alternate value of the parameter is returned. 

     Parameter Description:
parameter
description
expression
We must value to be tested
alt_value
Must, expression expression is NULL return value

COALESCE () function

       COALESCE (value, ...) is a function of the variable parameter, multiple parameters may be used.

      Role: receiving a plurality of parameters, return from left to right, the first parameter is not NULL, and if all parameters are NULL, then return NULL; when it uses two parameters, and the same effect IFNULL function.

     For chestnut:

SELECT IFNULL(NULL,'test'); //test
SELECT IFNULL('Hello','test'); //Hello
SELECT IFNULL(NULL,NULL); //NULL

SELECT COALESCE('Hello','test'); //Hello
SELECT COALESCE(NULL,'test'); //test
SELECT COALESCE('Hello','test','test1','test2'); //Hello
SELECT COALESCE(NULL,NULL,NULL,'test2'); //test2
SELECT COALESCE(NULL,NULL,NULL,NULL); //NULL

 

Guess you like

Origin www.cnblogs.com/east7/p/11706740.html