Functions about null in MySql
- IFNULL
- ISNULL
- NULLIF
IFNULL
Usage: IFNULL(expr1, expr2)
Description: If expr1 is not null, return expr1, otherwise return expr2
Example:
sql | result |
---|---|
SELECT IFNULL(null,'The first parameter is null') | 'The first argument is null' |
SELECT IFNULL('The first parameter is not null', 'I am the second parameter') | 'The first argument is not null' |
ISNULL
Usage: ISNULL(expr)
Description: If expr is null, return 1, otherwise return 0
Example:
sql | result |
---|---|
SELECT ISNULL(null) | 1 |
SELECT ISNULL('parameter not null') | 0 |
NULLIF
Usage: NULLIF(expr1, expr2)
Description: If expr1 is equal to expr2, return null. Otherwise return exp1. Like CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END.
example:
sql | result |
---|---|
SELECT NULLIF(1,1) | null |
SELECT NULLIF(1,2) | 1 |