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
In the past few years, DIV+CSS was very popular in...
Table of contents What is the reason for the sudd...
Table of contents 1. Plugins 2. Interlude 3. Impl...
This article describes how to use MySQL to export...
1. Use frameset, frame and iframe to realize mult...
Generally, when we use a table, we always give it...
Preface The keywords of MySQL and Oracle are not ...
In the previous article https://www.jb51.net/arti...
I haven't written a blog for a long time. Las...
The textarea tag is an HTML tag that we often use....
Index merging is an intelligent algorithm provide...
Today, when I was installing CentOS6.2, I couldn&...
reduce method is an array iteration method. Unlik...
The most important interactive design article in ...
Transactions ensure the atomicity of multiple SQL...