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
Table of contents Components - Timeline Custom no...
Detailed explanation of MySQL instance with SSD s...
Table of contents 0x01 Failed to load the driver ...
1. The div css mouse hand shape is cursor:pointer;...
1. I searched for a long time on the Internet but...
Introduction to JWT What is JWT The full name is ...
Today I encountered a problem when I used Dockerf...
Table of contents Introduction to bootstrap and i...
Table of contents 1. CDN introduction 1.1 react (...
1. Change password 1. Modify the password of ordi...
Table of contents 1. Master-slave synchronization...
This tutorial introduces the application of vario...
When Mysql associates two tables, an error messag...
I just learned some html yesterday, and I couldn&#...
Preface As a heavy user of front-end frameworks, ...