Home  >  Article  >  Database  >  How do we differentiate between MySQL IFNULL() and NULLIF() functions?

How do we differentiate between MySQL IFNULL() and NULLIF() functions?

王林
王林forward
2023-08-23 23:01:111237browse

我们如何区分 MySQL IFNULL() 和 NULLIF() 函数?

In fact, the syntax of MySQL IFNULL() and NULLIF() functions is almost the same, as shown below-

The syntax of IFNULL()
IFNULL(expression1, expression2)

NULLIF() Syntax

NULLIF(expression1, expression2)

You can distinguish them by returning the first parameter as the result. The IFNULL() function returns the first argument as the result if the first argument is not NULL; the NULLIF() function returns the first argument as the result if the two arguments are not the same.

mysql> Select IFNULL('Ram','Shyam');
+-----------------------+
| IFNULL('Ram','Shyam') |
+-----------------------+
| Ram                   |
+-----------------------+
1 row in set (0.00 sec)

mysql> Select NULLIF('Ram','Shyam');
+-----------------------+
| NULLIF('Ram','Shyam') |
+-----------------------+
| Ram                   |
+-----------------------+
1 row in set (0.00 sec)

Looking at the result sets of the two functions above, they look similar, but the IFNULL() function returns "Ram" because it is its first parameter and it is not NULL. On the other hand, the NULLIF() function returns "Ram" because it is the first parameter and is different from the second parameter.

The above is the detailed content of How do we differentiate between MySQL IFNULL() and NULLIF() functions?. For more information, please follow other related articles on the PHP Chinese website!

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