MySql sharing of null function usage

MySql sharing of null function usage

Functions about null in MySql

IFNULL

ISNULL

NULLIF

IFNULL

Usage: IFNULL(expr1,expr2)

Description: If expr1 is not null, return expr1, otherwise return expr2

example:

sql result
SELECT IFNULL(null,'The first parameter is null') 'The first parameter is null'
SELECT IFNULL('The first parameter is not null', 'I am the second parameter') 'The first parameter is not null'

ISNULL

Usage: ISNULL(expr)

Description: If expr is null, it returns 1, otherwise it returns 0

example:

sql result
SELECT ISNULL(null) 1
SELECT ISNULL('parameter that is not null') 0

NULLIF

Usage: NULLIF(expr1,expr2)

Description: If expr1 is equal to expr2, it returns null. Otherwise returns exp1. Similar to CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END.

example:

sql result
SELECT NULLIF(1,1) null
SELECT NULLIF(1,2) 1

Notes on using the MySql function IFNULL

First, create a simple table for SQL statement operations

The table creation statement is as follows:

CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key' ,
`name` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT 'Name' ,
`score` int(4) NOT NULL DEFAULT 0 COMMENT 'score' ,
PRIMARY KEY (`id`)
);

Manually create data as follows:

Now let’s get down to business:

a. What is the function of IFNULL? The following is a simple SQL statement and result. If IFNULL(a,b), the value received by a is null, then b is returned, otherwise a is returned.

SELECT IFNULL(NULL,0);

b. You can predict the result of the following SQL statement. According to the function of IFNULL function, it should return 0, but the result is not like this.

SELECT IFNULL(score,0) FROM student WHERE ID = 4;

The returned result is null, which is inconsistent with the expected result of 0.

c. The following statement returns the correct result 0;

SELECT IFNULL((SELECT score FROM student WHERE ID = 4),0);

Summarize:

When using method b to use IFNULL, SUM functions, etc., you need to ensure that there are query records, otherwise a null value will be returned. Of course, you can also use method c to avoid returning a null value and avoid NPE exceptions in the program.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL process control IF(), IFNULL(), NULLIF(), ISNULL() functions
  • This article takes you to explore NULL in MySQL
  • MySQL series of experience summary and analysis tutorials on NUll values
  • mysql IS NULL using index case explanation
  • Storing NULL values ​​on disk in MySQL

<<:  The ultimate solution for playing background music in Firefox browser (Chrome multi-browser compatible)

>>:  Summary of pitfalls of using nginx as a reverse proxy for grpc

Recommend

How to use docker to deploy Django technology stack project

With the popularity and maturity of Docker, it ha...

Ubuntu 20.04 CUDA & cuDNN Installation Method (Graphical Tutorial)

CUDA installation download cuda Enter the nvidia-...

The difference and usage of LocalStorage and SessionStorage in vue

Table of contents What is LocalStorage What is Se...

The difference between KEY, PRIMARY KEY, UNIQUE KEY, and INDEX in MySQL

The problem raised in the title can be broken dow...

How to use VUE to call Ali Iconfont library online

Preface Many years ago, I was a newbie on the ser...

Use iframe to submit form without refreshing the page

So we introduce an embedding framework to solve th...

How to connect Navicat to the docker database on the server

Start the mysql container in docekr Use command: ...

How to use nginx as a proxy cache

The purpose of using cache is to reduce the press...

Detailed explanation of js event delegation

1. Each function is an object and occupies memory...

MySQL foreign key setting method example

1. Foreign key setting method 1. In MySQL, in ord...

Debian virtual machine created by VirtualBox shares files with Windows host

the term: 1. VM: Virtual Machine step: 1. Downloa...

Detailed explanation of MySQL database index

Table of contents 1. Introduction to MySQL Index ...

Solution for Nginx installation without generating sbin directory

Error description: 1. After installing Nginx (1.1...

Example code for Html layered box-shadow effect

First, let’s take a look at the picture: Today we...