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

Detailed explanation of pure SQL statement method based on JPQL

JPQL stands for Java Persistence Query Language. ...

Some things to note about varchar type in Mysql

Storage rules for varchar In versions below 4.0, ...

How to set up vscode remote connection to server docker container

Table of contents Pull the image Run the image (g...

Docker completely deletes private library images

First, let’s take a look at the general practices...

A brief analysis of MySQL locks and transactions

MySQL itself was developed based on the file syst...

A brief discussion of 3 new features worth noting in TypeScript 3.7

Table of contents Preface Optional Chaining Nulli...

Introduction to JavaScript built-in objects

Table of contents 1. Built-in objects 2. Math Obj...

Basic structure of HTML documents (basic knowledge of making web pages)

HTML operation principle: 1. Local operation: ope...

How to assign a public IP address to an instance in Linux

describe When calling this interface, you need to...

Pure CSS custom multi-line ellipsis problem (from principle to implementation)

How to display text overflow? What are your needs...

How to use Baidu Map API in vue project

Table of contents 1. Register an account on Baidu...

A brief discussion on spaces and blank lines in HTML code

All consecutive spaces or blank lines (newlines) ...

Detailed explanation of MySQL's Seconds_Behind_Master

Table of contents Seconds_Behind_Master Original ...