Detailed explanation of IFNULL() and COALESCE() functions that replace null in mysql_Mysql

微波
Release: 2017-06-28 15:44:50
Original
2161 people have browsed it

This article mainly introduces to you about the IFNULL() and COALESCE()functionsinmysqlthat replacenullThe relevant information is introduced in great detail through example code in the article, which has certain reference and learning value for everyone. Friends who need it can take a look below.

In MySQLisnull()The function cannot be used as a replacement for null values!

is as follows:

First there is a table named business:


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

If you run it directly, an error will be reported:

Error code: 1582

Incorrect parameter count in the call to native function 'isnull'
Copy after login

So, theisnull()function will not work in mysql. You can useifnull()andcoalesce()instead. As follows:

Useifnull()function:

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

Running results:


When the value ofqueryis not null:

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

The result is as follows:


Use thecoalesce()function:

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

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
Copy after login


Among them:coalesce()can also return the first value that is not null. As follows:

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


So, how to useisnull()in mysql? The answer is to use it after where. As follows:

SELECT * FROM business WHERE ISNULL(business_name)
Copy after login

The result is as follows:


Similarly,is nullandis not nullis also used after where.

SELECT * FROM business WHERE business_name IS NULL
Copy after login

The results are as follows:

##

SELECT * FROM business WHERE business_name IS NOT NULL
Copy after login

Summary

The above is the detailed content of Detailed explanation of IFNULL() and COALESCE() functions that replace null in mysql_Mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!