Implementation of MySQL custom list sorting by specified field

Implementation of MySQL custom list sorting by specified field

Problem Description

As we all know, the SQL to sort in ascending order by a field in MySQL is (taking id as an example, the same below):

SELECT * FROM `MyTable`
WHERE `id` IN (1, 7, 3, 5)
ORDER BY `id` ASC

The SQL for descending order is:

SELECT * FROM `MyTable`
WHERE `id` IN (1, 7, 3, 5)
ORDER BY `id` DESC

Sometimes the above sorting does not meet our needs. For example, we want to sort by id in the order of 5, 3, 7, 1, how to achieve it? This is also one of the problems often encountered by many domestic and foreign counterparts.

Below we give a solution to sort a field in the table in the list format we want.

Solution

Use "ORDER BY FIELD".

grammar

ORDER BY FIELD(`id`, 5, 3, 7, 1)

Note that there is no space after FIELD.

Therefore, the complete SQL is:

SELECT * FROM `MyTable`
WHERE `id` IN (1, 7, 3, 5)
ORDER BY FIELD(`id`, 5, 3, 7, 1)

Common Applications

SELECT * FROM `MyTable`
WHERE `name` IN ('张三', '李四', '王五', '孙六')
ORDER BY FIELD(`name`, '李四', '孙六', '张三', '王五')

The above article on how to implement MySQL custom list sorting by specified fields is all I want to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Mysql method to copy a column of data in one table to a column in another table
  • MySQL FAQ series: When to use temporary tables
  • Sql query MySql database table name and description table field (column) information
  • An example of how to use Java+MySQL recursion to concatenate tree-shaped JSON lists
  • Summary of Mysql table, column, database addition, deletion, modification and query problems
  • How to get the field list after querying the results of the Python module pymysql
  • MySQL table and column comments summary

<<:  Detailed explanation of Vite's new experience

>>:  How to use vite to build vue3 application

Recommend

How to upgrade https under Nginx

Purchase Certificate You can purchase it from Ali...

The difference and usage of Ctrl+z, Ctrl+c and Ctrl+d in Linux commands

What does Ctrl+c, Ctrl+d, Ctrl+z mean in Linux? C...

How to quickly paginate MySQL data volumes of tens of millions

Preface In backend development, in order to preve...

15 Vim quick reference tables to help you increase your efficiency by N times

I started using Linux for development and enterta...

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...

How to modify the IP restriction conditions of MySQL account

Preface Recently, I encountered a requirement at ...

Basic introductory tutorial on MySQL partition tables

Preface In a recent project, we need to save a la...

Linux system prohibits remote login command of root account

ps: Here is how to disable remote login of root a...

Introduction to Vue life cycle and detailed explanation of hook functions

Table of contents Vue life cycle introduction and...

Detailed explanation of the implementation of MySQL auto-increment primary key

Table of contents 1. Where is the self-incremente...

HTML form tag tutorial (5): text field tag

<br />This tag is used to create a multi-lin...

HTML Code Writing Guide

Common Convention Tags Self-closing tags, no need...

WeChat Mini Program Basic Tutorial: Use of Echart

Preface Let’s take a look at the final effect fir...