Detailed explanation of the use of IF(), IFNULL(), NULLIF(), and ISNULL() functions in MySQL

Detailed explanation of the use of IF(), IFNULL(), NULLIF(), and ISNULL() functions in MySQL

In MySQL, you can use IF(), IFNULL(), NULLIF(), and ISNULL() functions to control the process.

1. Use of IF() function

IF(expr1, expr2, expr3) returns the value of expr2 if the value of expr1 is true, and returns the value of expr3 if the value of expr1 is false.

SELECT IF(TRUE,'A','B'); -- Output: A
SELECT IF(FALSE,'A','B'); -- Output: B

2. Use of IFNULL() function

IFNULL(expr1, expr2) returns the value of expr2 if the value of expr1 is null, and returns the value of expr1 if the value of expr1 is not null.

Example 1

      mysql> SELECT IFNULL(1,0);   
                            -> 1   
      mysql> SELECT IFNULL(NULL,10);   
                            -> 10   
      mysql> SELECT IFNULL(1/0,10);   
                            -> 10   
      mysql> SELECT IFNULL(1/0,'yes');   
                            -> 'yes'  

Example 2

SELECT IFNULL(NULL,'B'); -- Output: B
SELECT IFNULL('HELLO','B'); -- Output: HELLO

3. Use of NULLIF() function

NULLIF(expr1, expr2), if expr1=expr2, the return value is null, otherwise the return value is the value of expr1.

SELECT NULLIF('A','A'); -- Output: null
SELECT NULLIF('A','B'); -- Output: A

4. Use of ISNULL() function

ISNULL(expr) returns 1 if the value of expr is null, and returns 0 if the value of expr1 is not null.

SELECT ISNULL(NULL); -- Output: 1
SELECT ISNULL('HELLO'); -- Output: 0

This is the end of this article about the detailed usage of IF(), IFNULL(), NULLIF(), and ISNULL() functions in MySQL. For more information about IF(), IFNULL(), NULLIF(), and ISNULL() in MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of IFNULL, NULLIF and ISNULL usage in MySql
  • MySQL process control IF(), IFNULL(), NULLIF(), ISNULL() functions

<<:  Detailed explanation of application scenarios of filters in Vue

>>:  Two ways to manage volumes in Docker

Recommend

HTML table markup tutorial (16): title horizontal alignment attribute ALIGN

By default, the table title is horizontally cente...

How to set up a deployment project under Linux system

1. Modify the firewall settings and open the corr...

Detailed explanation of the buffer pool in MySQL

Everyone knows that data in MySQL needs to be wri...

In-depth analysis of the role of HTML <!--...--> comment tags

When we check the source code of many websites, w...

Detailed explanation of 7 SSH command usages in Linux that you don’t know

A system administrator may manage multiple server...

JavaScript to implement limited time flash sale function

This article shares the specific code of JavaScri...

Vue implements adding watermark effect to the page

Recently, when I was working on a project, I was ...

Page Refactoring Skills - Content

Enough of small talk <br />Based on the lar...

Specific use of Docker anonymous mount and named mount

Table of contents Data volume Anonymous and named...

How a select statement is executed in MySQL

Table of contents 1. Analyzing MySQL from a macro...

Introduction to new features of ECMAscript

Table of contents 1. Default values ​​for functio...

Summary of 6 Linux log viewing methods

As a backend programmer, you deal with Linux in m...

Centos7 installation of MySQL8 tutorial

MySQL 8 new features: My personal opinion on MySQ...