Preface 1. PreparationFor better demonstration and understanding, first prepare a student table, add three fields: number, name, and grade, and insert several pieces of data, as shown in the figure: 2. Conditional sortingRequirement 1: Sort the scores from high to low Even the aunties selling vegetables on the street can knock, and you can easily complete it by using Requirement 2: Sort the scores from high to low, and those without scores are at the front Customer experience is the most important. It is normal to make such a request to facilitate the second entry of scores. To achieve this sorting, the above statement cannot be implemented, so conditional sorting is needed. First, determine if the score is empty and assign a maximum value, and then sort. For example
3. Custom sortingCustomers are like gods, and it is not uncommon to see fancy demands. For example, they require Zhang San and Li Si to be at the front, and other students to be ranked from high to low according to their grades. In this case, you need to use custom sorting, and the MySQL built-in FIELD function (returns the index of the corresponding string) can help you achieve it.
The result of running the above statement puts Zhang San and Li Si at the end (as shown in the left picture below). Naturally, we should put Zhang San and Li Si at the front and sort them in descending order.
The result was exactly what he wanted (as shown in the right picture below).
Later, I found that the FIND_IN_SET function can also be used to achieve this, and after testing with 1 million data records, FIND_IN_SET has better performance.
4. Sorting Chinese characters by their first letters Some friends may wonder why it is so little known since Chinese characters can be easily sorted using the common
SummarizeThis concludes this article about some lesser-known sorting methods in MySQL. For more information about MySQL sorting methods, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: URL representation in HTML web pages
>>: Docker container accesses the host's MySQL operation
The local environment is Windows 10 + WSL2 (Ubunt...
Basic syntax: <input type="hidden" na...
Quick Reading Why do we need to monitor SQL state...
1. Network Optimization YSlow has 23 rules. These...
Take the deployment of https://gitee.com/tengge1/...
When it comes to tool-type websites, we first hav...
Table of contents 01 Common controllers in k8s RC...
This article example shares the specific code of ...
1. Inline reference: used directly on the label, ...
Table of contents Preface Can typeof correctly de...
This article describes how to install mysql5.6 us...
Today, when I searched for a page on Baidu, becaus...
Table of contents Code cleaning "Frames"...
Table of contents Preface 1. Optimistic Locking A...
Table of contents Problems Redux Toolkit solves W...