Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the IFNULL (), ISNULL () and NULLIF () functions of MySQL

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to use MySQL's IFNULL (), ISNULL () and NULLIF () functions". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor show you how to use the IFNULL (), ISNULL () and NULLIF () functions of MySQL.

I. usage of IFNULL

1.1 IFNULL (expr1,expr2) usage

If expr1 is not NULL, the return value of IFNULL () is expr1; otherwise its return value is expr2. The return value of IFNULL () is a number or a string, depending on the context in which it is used.

Mysql > SELECT IFNULL (1J0)

-> 1

Mysql > SELECT IFNULL (NULL,10)

-> 10

Mysql > SELECT IFNULL (1gamma 0pr 10)

-> 10

Mysql > SELECT IFNULL (1gamma 0pensionyes`)

-> 'yes'

The default result value of IFNULL (expr1,expr2) is the more "generic" of the two expressions, in the order STRING, REAL, or INTEGER.

1.2 usage of isnull (expr):

If expr is null, the return value of isnull () is 1, otherwise the return value is 0.

Mysql > select isnull (1x1)

-> 0

Mysql > select isnull (1thumb 0)

-> 1

It is usually wrong to compare with the null value of =.

The isnull () function has some of the same characteristics as the isnull comparison operator. See the instructions for is null.

II. The usage of ISNULL (expr)

If expr is null, the return value of isnull () is 1, otherwise the return value is 0.

Mysql > select isnull (1x1)

-> 0

Mysql > select isnull (1thumb 0)

-> 1

It is usually wrong to compare with the null value of =.

III. The usage of NULLIF (expr1,expr2)

If expr1 = expr2 is established, the return value is NULL, otherwise the return value is expr1. This is the same as CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END.

Mysql > SELECT NULLIF (1Pol 1)

-> NULL

Mysql > SELECT NULLIF (1Jue 2)

-> 1

If the parameters are not equal, the value obtained by MySQL twice is expr1.

At this point, I believe you have a deeper understanding of "how to use MySQL's IFNULL (), ISNULL (), and NULLIF () functions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report