Detailed explanation of IFNULL() and COALESCE() functions to replace null in MySQL

Detailed explanation of IFNULL() and COALESCE() functions to replace null in MySQL

The isnull() function cannot be used as a substitute for null values ​​in MySQL!

as follows:

First, there is a table named business:

SELECT ISNULL(business_name,'no business_name') AS bus_isnull FROM business WHERE id=2

Running it directly will give an error:

Error code: 1582

Incorrect parameter count in the call to native function 'isnull'

Therefore, the isnull() function does not work in MySQL. You can use ifnull() and coalesce() instead. as follows:

Use ifnull() function:

SELECT IFNULL(business_name,'no business_name') AS bus_ifnull FROM business WHERE id=2

Running results:


When the queried value is not null:

SELECT IFNULL(business_name,'no business_name') AS bus_ifnull FROM business WHERE id=1

The results are as follows:


Use coalesce() function:

SELECT COALESCE(business_name,'no business_name') AS bus_coalesce FROM business WHERE id=2

The results are as follows:


When the query value is not null:

SELECT COALESCE(business_name,'no business_name') AS bus_coalesce FROM business WHERE id=1 

Among them: coalesce() can also return the first non-null value. as follows:

SELECT COALESCE(business_name,district_id,id) AS bus_coalesce FROM business WHERE id=2 

So, how to use isnull() in MySQL? The answer is used after where. as follows:

SELECT * FROM business WHERE ISNULL(business_name)

The results are as follows:


Similarly, is null and is not null are also used after where.

SELECT * FROM business WHERE business_name IS NULL

The results are as follows:

SELECT * FROM business WHERE business_name IS NOT NULL 

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Solutions to invalid is Null segment judgment and IFNULL() failure in MySql
  • Solution to MySQL IFNULL judgment problem
  • Summary of knowledge points related to null (IFNULL, COALESCE and NULLIF) in MySQL
  • A brief discussion on ifnull() function similar to nvl() function in MySQL
  • Detailed explanation of IFNULL, NULLIF and ISNULL usage in MySql
  • A brief discussion on the usage of SQL Server's ISNULL function and MySQL's IFNULL function
  • Introduction to the difference between IFNULL, IF, and CASE in MySQL
  • Instructions for nested use of MySQL ifnull

<<:  How to use file writing to debug a Linux application

>>:  Complete code for implementing the popular astronaut watch face based on JavaScript

Recommend

Interpretation and usage of various React state managers

First of all, we need to know what a state manage...

Detailed application of Vue dynamic form

Overview There are many form requirements in the ...

MySQL partition table is classified by month

Table of contents Create a table View the databas...

CSS uses calc() to obtain the current visible screen height

First, let's take a look at the relative leng...

JavaScript canvas to achieve mirror image effect

This article shares the specific code for JavaScr...

Design of image preview in content webpage

<br />I have written two articles before, &q...

Detailed explanation of how to use the Vue date time picker component

This article example shares the specific code of ...

Analysis of the principles and usage of Linux hard links and soft links

In the Linux system, there is a kind of file call...

Vue defines private filters and basic usage

The methods and concepts of private filters and g...

HTML optimization techniques you must know

To improve the performance of web pages, many dev...

Implementation of JavaScript downloading linked images and uploading them

Since we are going to upload pictures, the first ...

Navicat imports csv data into mysql

This article shares with you how to use Navicat t...

Docker image management common operation code examples

Mirroring is also one of the core components of D...

User experience analysis of facebook dating website design

<br />Related article: Analysis of Facebook&...