Example of using CASE WHEN in MySQL sorting

Example of using CASE WHEN in MySQL sorting

Preface

In a previous project, the CASE WHEN sorting function in SQL was used. Now write a blog memo~

Database version: MySQL 5.6.42

condition:

A field represents the state of the data and takes a non-negative integer value, where 0 means no state.

need:

Sort by this field in ascending order, and put the data with a value of 0 at the end.

First, let's look at the structure of the table:

The normal query result in ascending order is as follows:

To put "小赤" at the end, you can write:

You can also do this:

You can also use CASE WHEN to change the sorting basis without affecting the query results:

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:
  • Instructions for using the MySQL CASE WHEN statement
  • Several examples of using MySQL's CASE WHEN statement
  • How to use case when statement in MySQL to implement multi-condition query
  • The pitfall record of case when judging NULL value in MySQL

<<:  How to smoothly upgrade and rollback Nginx version in 1 minute

>>:  Vue uses custom instructions to add watermarks to the bottom of the page

Recommend

Analysis of Hyper-V installation CentOS 8 problem

CentOS 8 has been released for a long time. As so...

VMware15/16 Detailed steps to unlock VMware and install MacOS

VMware version: VMware-workstation-full-16 VMware...

Detailed example of database operation object model in Spring jdbc

Detailed example of database operation object mod...

Detailed explanation of how to use $props, $attrs and $listeners in Vue

Table of contents background 1. Document Descript...

Docker deployment springboot project example analysis

This article mainly introduces the example analys...

Detailed explanation of MySQL index selection and optimization

Table of contents Index Model B+Tree Index select...

Understand CSS3 Grid layout in 10 minutes

Basic Introduction In the previous article, we in...

Execute initialization sql when docker mysql starts

1. Pull the Mysql image docker pull mysql:5.7 2. ...

In-depth understanding of the matching logic of Server and Location in Nginx

Server matching logic When Nginx decides which se...

Detailed use cases of MySql escape

MySQL escape Escape means the original semantics ...

Image scrolling effect made with CSS3

Achieve resultsImplementation Code html <base ...

Discussion on more reasonable creation rules for MySQL string indexes

Preface Regarding the use of MySQL indexes, we ha...