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` The SQL for descending order is: SELECT * FROM `MyTable` 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` Common Applications SELECT * FROM `MyTable` 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:
|
<<: Detailed explanation of Vite's new experience
>>: How to use vite to build vue3 application
Purchase Certificate You can purchase it from Ali...
Win10 installation (skip if already installed) Fo...
What does Ctrl+c, Ctrl+d, Ctrl+z mean in Linux? C...
Preface In backend development, in order to preve...
I started using Linux for development and enterta...
Under the requirements of today's responsive ...
Preface Recently, I encountered a requirement at ...
Preface In a recent project, we need to save a la...
ps: Here is how to disable remote login of root a...
Table of contents Vue life cycle introduction and...
Table of contents 1. Where is the self-incremente...
<br />This tag is used to create a multi-lin...
Common Convention Tags Self-closing tags, no need...
1. The mysqldump backup method uses logical backu...
Preface Let’s take a look at the final effect fir...