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

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

王林
Release: 2023-08-23 23:01:11
forward
1300 people have browsed it

我们如何区分 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)
Copy after login

NULLIF() Syntax

NULLIF(expression1, expression2)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template