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 add file prefixes in batches in Linux

You need to add "gt_" in front of the f...

Installation of Docker CE on Ubuntu

This article is used to record the installation o...

Detailed tutorial on installing mysql under Linux

1. Shut down the mysql service # service mysqld s...

Can you do all the web page making test questions?

Web page design related questions, see if you can...

It's the end of the year, is your MySQL password safe?

Preface: It’s the end of the year, isn’t it time ...

Detailed explanation of setting up DNS server in Linux

1. DNS server concept Communication on the Intern...

Solution to secure-file-priv problem when exporting MySQL data

ERROR 1290 (HY000) : The MySQL server is running ...

MySQL 8.x msi version installation tutorial with pictures and text

1. Download MySQL Official website download addre...

Vue implements countdown between specified dates

This article example shares the specific code of ...

Start nginxssl configuration based on docker

Prerequisites A cloud server (centOS of Alibaba C...

Summary of MySQL foreign key constraints and table relationships

Table of contents Foreign Key How to determine ta...

JS quickly master ES6 class usage

1. How to construct? Let's review the common ...

How to enter directory/folder in Linux without using CD command

As we all know, without the cd command, we cannot...

Robots.txt detailed introduction

Basic introduction to robots.txt Robots.txt is a p...