Get the App
SLTechnology News&Howtos  ›  Database  › 

Example of using CASE WHEN in MySQL sorting

Shulou Source: shulou.com Published: 2022-06-01 04:01:38 09月10日 Update

preface

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

Database version: MySQL 5.6.42

Conditions:

A field indicates that the state of the data is a non-negative integer, and 0 indicates no state.

Requirements:

Sort in ascending order for this field, and you need to put data with a value of 0 last.

First let's look at the table structure:

The normal result of an ascending query is:

To put "Little Red" at the end of the list, write:

It can also be like this:

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

summary

The above is the whole content of this article, I hope the content of this article has a certain reference value for everyone's study or work, if you have questions, you can leave a message to exchange, thank you for your support.

Tags: Sort data content ascending order field status result learning query representative value no more premise preface function blog and at the same time database integer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS Shulou Technology Huawei OPPO Reno