为了账号安全,请及时绑定邮箱和手机立即绑定

MySQL IFNULL

标签:
MySQL

Summary: in this tutorial, you will learn about the MySQL IFNULL function, which is a very handy control flow function in MySQL.

Introduction to MySQL IFNULL function

MySQL IFNULL function is one of the MySQL control flow functions that accepts two arguments and returns the first argument if the argument is not NULL; otherwise it returns the second argument. Arguments can be literal values or expressions.

The following illustrates the syntax of the IFNULL function:

IFNULL(expression_1,expression_2)

The IFNULL function returns expression_1 if expression_1 is not NULL; otherwise it returns expression_2. The IFNULL function returns a string or a numeric based on the context where it is used.

If you want return a value based on TRUE or FALSE condition other than NULL, you should use the IF function.

MySQL IFNULL function examples

See the following IFNULL function examples:

SELECT IFNULL(1,0); -- returns 1 SELECT IFNULL('',1); -- returns '' SELECT IFNULL(NULL,'IFNULL function'); -- returns IFNULL function

How it works.

  • IFNULL(1,0) returns 1 because 1 is not NULL.

  • IFNULL(' ',1) returns ' ' because ' ' string is not NULL.

  • IFNULL(NULL,'IFNULL function') returns IFNULL function string because the first argument is NULL.

Let’s take a practical example of using the IFNULL function.

First, create a new table named contacts by using the CREATE TABLE statement:

CREATE TABLE IF NOT EXISTS contacts(     contactid INT(4) AUTO_INCREMENT PRIMARY KEY,     contactname varchar(20) NOT NULL,     bizphone varchar(15),     homephone varchar(15) )ENGINE=InnoDB;

Each contact has contact name, business phone and home phone.

Second, insert data into the contacts table by using the INSERT statement:

INSERT INTO contacts(contactname,bizphone,homephone) VALUES('John Doe','(541) 754-3009',NULL), ('Cindy Smith',NULL,'(541) 754-3110'), ('Sue Greenspan','(541) 754-3010','(541) 754-3011'), ('Lily Bush',NULL,'(541) 754-3111');

Some contacts have only home phone or business phone. To get all the contact name and phone from the contactstable, you use the following query:

SELECT contactName,        bizphone,        homephone FROM contacts;

MySQL IFNULL - Contacts table

However, it would be nice if we can get the contact’s home phone if the contact’s business phone is not available. This is where the IFNULL function comes to play. The IFNULL function returns home phone if the business phone is  NULL.

Third, use the following query to get the names and phones of all the contacts:

SELECT contactname,        IFNULL(bizphone,homephone) phone FROM contacts;

MySQL IFNULL - Contacts with phone

Notice that you should avoid using the IFNULL function in the WHERE clause, because it degrades the performance of the query. If you want to check if a value is NULL or not, you can use IS NULL or IS NOT NULL in the WHEREclause.

In this tutorial, we have introduced you to MySQL IFNULL function and shown you how to use the IFNULL function in the queries.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/function/mysql-ifnull

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消