Home >Database >Mysql Tutorial >How to use IFNULL, NULLIF and ISNULL in MySql

How to use IFNULL, NULLIF and ISNULL in MySql

WBOY
WBOYforward
2023-04-17 17:52:061705browse

1. The expression of IFNULL is as follows:

IFNULL(expr1,expr2)

Function description: If expr1 is null, the function returns expr2, otherwise it will Return expr1.

Example 1:

SELECT IFNULL(0,'ab');

How to use IFNULL, NULLIF and ISNULL in MySql

The first parameter is 0, not NULL, so the result is 0.

Example 2:

SELECT IFNULL(NULL,'ab');

How to use IFNULL, NULLIF and ISNULL in MySql

The first parameter is NULL, so the result is ab.

2. The expression of NULLIF is as follows:

SELECT NULLIF(expr1,expr2)

Function description: If two If the two parameters are equal, NULL is returned; otherwise, the first parameter is returned.

Example 1:

SELECT NULLIF('a','b');

How to use IFNULL, NULLIF and ISNULL in MySql

The two parameters are not equal, so the result is the first parameter a.

Example 2:

SELECT NULLIF('a','a');

How to use IFNULL, NULLIF and ISNULL in MySql

The two parameters are equal, so the result is NULL.

3. The expression of ISNULL is as follows:

SELECT ISNULL(expr)

Function description: If expr is null, then isnull() The return value is 1, otherwise the return value is 0.

Example 1:

SELECT ISNULL(NULL);

How to use IFNULL, NULLIF and ISNULL in MySql

The parameter is NULL, so the result is 1.

Example 2:

SELECT ISNULL('ab');

How to use IFNULL, NULLIF and ISNULL in MySql

The parameter is not NULL, so the result is 0.

The above is the detailed content of How to use IFNULL, NULLIF and ISNULL in MySql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete