MySQL NULL data conversion method (must read)

MySQL NULL data conversion method (must read)

When using MySQL to query the database and executing left join , some of the associated fields have NULL contents. Therefore, after obtaining the record set, the NULL data needs to be converted.

This article will provide a method to perform the conversion processing directly at query time. The acquired record set does not need to be converted again.

mysql provides the IFNULL function

IFNULL(expr1, expr2)

IFNULL() returns expr1 if expr1 is not NULL, otherwise it returns expr2

Examples:

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 user's name and last login time

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 never logged in, there is no record in the user_lastlogin table. Therefore lastlogintime is NULL.

Use IFNULL to convert NULL to 0

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 article on MySQL conversion of NULL data (must read) is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of NULL values ​​in MySQL
  • Solution to the problem of null column in NOT IN filling pit in MySQL
  • MySQL NULL value processing example detailed explanation
  • MySQL series of experience summary and analysis tutorials on NUll values

<<:  Detailed explanation of Nginx access restriction configuration

>>:  How to query data within a certain period of time with Vue front-end and Django back-end

Recommend

Detailed introduction to nobody user and nologin in Unix/Linux system

What is the nobody user in Unix/Linux systems? 1....

Vue+video.js implements video playlist

This article shares the specific code of vue+vide...

How to use css variables in JS

How to use css variables in JS Use the :export ke...

Does Mysql ALTER TABLE lock the table when adding fields?

Table of contents Before MySQL 5.6 After MySQL 5....

JavaScript object-oriented implementation of magnifying glass case

This article shares the specific code of JavaScri...

Solution to forgetting mysql database password

You may have set a MySQL password just now, but f...

How to install JDK8 on Windows

1. Download: http://www.oracle.com/technetwork/ja...

vue dynamic component

Table of contents 1. Component 2. keep-alive 2.1 ...

Usage and execution process of http module in node

What is the role of http in node The responsibili...

Mysql date formatting and complex date range query

Table of contents Preface Query usage scenario ca...

Solve the problem that ifconfig and addr cannot see the IP address in Linux

1. Install the Linux system on the virtual machin...

JavaScript Basics Series: Functions and Methods

Table of contents 1. The difference between funct...

Use Nginx to build a streaming media server to realize live broadcast function

Written in front In recent years, the live stream...

How to achieve 3D dynamic text effect with three.js

Preface Hello everyone, this is the CSS wizard - ...