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

Nginx domain forwarding usage scenario code example

Scenario 1: Due to server restrictions, only one ...

Nginx prohibits direct access via IP and redirects to a custom 500 page

Directly to the configuration file server { liste...

Sharing of experience on repairing MySQL innodb exceptions

A set of MySQL libraries for testing. The previou...

How to store false or true in MySQL

MySQL Boolean value, stores false or true In shor...

Detailed explanation of several ways to create a top-left triangle in CSS

Today we will introduce several ways to use CSS t...

Application of mapState idea in vuex

Table of contents 1. Map method 2. Application ba...

Real-time refresh of long connection on Vue+WebSocket page

Recently, the Vue project needs to refresh the da...

Use CSS to draw a file upload pattern

As shown below, if it were you, how would you ach...

VMware configuration VMnet8 network method steps

Table of contents 1. Introduction 2. Configuratio...

Win10 uses Tsinghua source to quickly install pytorch-GPU version (recommended)

Check whether your cuda is installed Type in the ...

Introduction and examples of hidden fields in HTML

Basic syntax: <input type="hidden" na...

Vue implements two-way data binding

This article example shares the specific code of ...