Mysql implements null value first/last method example

Mysql implements null value first/last method example

Preface

We already know that MySQL uses the SQL SELECT command and the WHERE clause to read data from a table, but when the query condition field provided is NULL, the command may not work properly.

To handle this situation, MySQL provides three major operators:

  • IS NULL: This operator returns true when the column value is NULL.
  • IS NOT NULL: The operator returns true when the column value is not NULL.
  • <=>: A comparison operator (different from the = operator) that returns true if both values ​​being compared are NULL.

Conditional comparison operations on NULL are special. You cannot use = NULL or != NULL to look for NULL values ​​in a column.

In MySQL, a comparison of a NULL value with any other value (even NULL) always returns false, that is, NULL = NULL returns false.

MySQL handles NULL using the IS NULL and IS NOT NULL operators.

I'm currently migrating a project from Oracle to MySQL. I encountered some Oracle functions that MySQL doesn't have, so I had to customize the functions or find a way to replace the functions for the transformation.

When Oracle sorts data, it can sometimes use nulls first or nulls last to put null values ​​first or last.

Oracle method:

Null values ​​are sorted first

select * from A order by a desc null first

Null values ​​are sorted last

select * from A order by a desc null last

However, if you migrate to MySQL, MySQL does not provide a similar function, so how can you implement it?

Here is the solution:

Null values ​​are sorted last, using MySQL's IF and ISNULL functions. If it is empty, it returns 1, otherwise it returns 0

select * from A order by IF(ISNULL(a),1,0),a desc

Null values ​​are sorted first, using MySQL's IF and ISNULL functions. If it is empty, it returns 1, otherwise it returns 0

select * from A order by IF(ISNULL(a),0,1),a desc

If Oracle and MySQL versions are needed in mybatis, you can either pass a data table version identifier dbType from the background, or directly use the _databaseId method of mybatis.

 <if test="dbType=='oracle'">
   order by c.create_date desc nulls last
   </if>
   <if test="dbType=='mysql'">
   order by IF(ISNULL(c.create_date),1,0), c.create_date desc
   </if>

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Tutorial on understanding and using NULL values ​​in MySQL
  • Detailed explanation of NULL values ​​in MySQL
  • Distinguish between null value and empty character ('''') in MySQL
  • Instance method for mysql string concatenation and setting null value
  • MySQL NULL value processing example detailed explanation
  • Do you know the difference between empty value and null value in mysql
  • This article takes you to explore NULL in MySQL

<<:  VMware 15.5 version installation Windows_Server_2008_R2 system tutorial diagram

>>:  Detailed explanation of the usage of setUp and reactive functions in vue3

Recommend

SQL Server Comment Shortcut Key Operation

Batch comments in SQL Server Batch Annotation Ctr...

Ubuntu 20.04 Best Configuration Guide (Newbie Essential)

1. System Configuration 1. Turn off sudo password...

Docker exec executes multiple commands

The docker exec command can execute commands in a...

How to configure Nginx domain name rewriting and wildcard domain name resolution

This article introduces how to configure Nginx to...

Detailed explanation of the use of CSS pointer-events attribute

In front-end development, we are in direct contac...

MySQL 8.0.16 installation and configuration tutorial under Windows 10

This article shares with you the graphic tutorial...

When to use Map instead of plain JS objects

Table of contents 1. Map accepts any type of key ...

How to implement variable expression selector in Vue

Table of contents Defining the HTML structure Inp...

Sample code for implementing the Olympic rings with pure HTML+CSS

Rendering Code - Take the blue and yellow rings a...

Reasons and solutions for not being able to detect array changes in Vue2

Table of contents Workaround Why can't I moni...

HTML tutorial, easy to learn HTML language

1. <body background=image file name bgcolor=co...

Teach you how to quickly install Nginx in CentOS7

Table of contents 1. Overview 2. Download the Ngi...

jQuery implements navigation bar effect with expansion animation

I designed and customized a navigation bar with a...

Solution to Ubuntu 20.04 Firefox cannot play videos (missing flash plug-in)

1. Flash plug-in package download address: https:...