Example of using CASE WHEN in MySQL sorting
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.