Mysql to convert NULL data (mandatory), mysqlnull

Source: Internet
Author: User

Mysql to convert NULL data (mandatory), mysqlnull

Use mysql to query the database.Left joinSome associated fields areNULLTherefore, after obtaining the record set, you need to convert NULL data.

This article provides a way to directly execute the Conversion Processing during the query. The obtained record set does not need to be converted.

Mysql provides IFNULL Functions

IFNULL (expr1, expr2)

If expr1 is not NULL, IFNULL () returns expr1; otherwise, expr2 is returned.

Instance:

User table structure and data

+----+-----------+| id | name   |+----+-----------+| 1 | Abby   || 2 | Daisy   || 3 | Christine |+----+-----------+

User_lastlogin table structure and data

+-----+---------------+| uid | lastlogintime |+-----+---------------+|  1 |  1488188120 ||  3 |  1488188131 |+-----+---------------+

Query the name and lastlogintime of a user

Mysql> select a. id, a. name, B. lastlogintime from user as a left join user_lastlogin as B on a. id = B. uid;

+----+-----------+---------------+| id | name   | lastlogintime |+----+-----------+---------------+| 1 | Abby   |  1488188120 || 2 | Daisy   |     NULL || 3 | Christine |  1488188131 |+----+-----------+---------------+

Because the user with id = 2 has not logged in, there is no record in the user_lastlogin table. Therefore, lastlogintime is NULL.

Convert NULL to 0 using IFNULL

IFNULL(lastlogintime, 0)mysql> select a.id,a.name,IFNULL(b.lastlogintime,0) as lastlogintime from user as a left join user_lastlogin as b on a.id=b.uid;+----+-----------+---------------+| id | name   | lastlogintime |+----+-----------+---------------+| 1 | Abby   |  1488188120 || 2 | Daisy   |       0 || 3 | Christine |  1488188131 |+----+-----------+---------------+

The above mysql-converted NULL data method (mandatory) is all the content shared by the editor. I hope you can give us a reference and support the house of helpers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.