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

Detailed explanation of Linux text processing command sort

sort Sort the contents of a text file Usage: sort...

Steps to package and deploy the Vue project to the Apache server

In the development environment, the vue project i...

MySQL 5.7.17 installation and configuration graphic tutorial

The blogger said : I have been writing a series o...

Analysis of the solution to Nginx Session sharing problem

This article mainly introduces the solution to th...

Detailed explanation of four solutions to floating problems in CSS layout

1. Cause: The effect after the subbox is set to f...

Docker+nacos+seata1.3.0 installation and usage configuration tutorial

I spent a day on it before this. Although Seata i...

Use Javascript to develop sliding-nav navigation plug-in with sliding bar effect

Table of contents 1. Introduction 2. Usage 3. Dev...

MySQL database monitoring software lepus usage problems and solutions

When using lepus3.7 to monitor the MySQL database...

Solution to Docker image downloading too slowly

Docker image download is stuck or too slow I sear...

Examples of using MySQL covering indexes

What is a covering index? Creating an index that ...

The 6 Most Effective Ways to Write HTML and CSS

This article shares the 6 most effective methods,...

Text pop-up effects implemented with CSS3

Achieve resultsImplementation Code html <div&g...

Example code and method of storing arrays in mysql

In many cases, arrays are often used when writing...