Some lesser-known sorting methods in MySQL

Some lesser-known sorting methods in MySQL

Preface

ORDER BY 字段名升序/降序, I believe that everyone here knows this sorting statement, but when encountering some special sorting, using only the field name cannot meet the needs. Here are some sorting methods I have encountered:

1. Preparation

For 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 sorting

Requirement 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 ORDER BY examScore DESC (as shown in the left picture below).

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 ORDER BY IF(examScore IS NULL,101,examScore) DESC can also be easily implemented (as shown in the right figure below).

need Requirement 1 Requirement 2
Statements ORDER BY examScore DESC ORDER BY IF(examScore IS NULL,101,examScore) DESC
Effect insert image description hereinsert image description here

3. Custom sorting

Customers 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.

Statement 1:
ORDER BY FIELD(studentName,'张三','李四') ASC, examScore DESC;

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.

Statement 2:
ORDER BY FIELD(studentName,'李四','张三') DESC, examScore DESC;

The result was exactly what he wanted (as shown in the right picture below).

Statements Sentence 1 Statement 2
Effect insert image description hereinsert image description here

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.

ORDER BY FIND_IN_SET(studentName,'李四,张三') DESC, examScore DESC;

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 ORDER BY 字段ASC .
In fact, when users create table fields using the GBK character set, the problem can be solved by directly using ORDER BY 字段ASC . Some users use the utf8 character set to prevent garbled characters, and simple sorting statements are powerless (as shown in the left picture below). Therefore, it is OK to convert the fields to GBK when sorting (as shown in the right picture below).

Statements ORDER BY studentName ASC ORDER BY CONVERT(studentName USING GBK) ASC
Effect insert image description hereinsert image description here

Summarize

This 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:
  • Example of utf8mb4 collation in MySQL
  • MySQL aggregate function sorting
  • MySQL sorting using index scan
  • Mysql Chinese sorting rules description
  • Pitfalls based on MySQL default sorting rules
  • MySQL sorting principles and case analysis
  • MySQL query sorting and paging related
  • How to use indexes to optimize MySQL ORDER BY statements
  • Mysql sorting and paging (order by & limit) and existing pitfalls
  • MySQL sorting feature details

<<:  URL representation in HTML web pages

>>:  Docker container accesses the host's MySQL operation

Recommend

Introduction to Semantic HTML Tags

In the past few years, DIV+CSS was very popular in...

How to achieve the maximum number of connections in mysql

Table of contents What is the reason for the sudd...

Vue3.0 uses the vue-grid-layout plug-in to implement drag layout

Table of contents 1. Plugins 2. Interlude 3. Impl...

Analysis of MySQL's method of exporting to Excel

This article describes how to use MySQL to export...

PHP-HTMLhtml important knowledge points notes (must read)

1. Use frameset, frame and iframe to realize mult...

HTML table border control implementation code

Generally, when we use a table, we always give it...

Detailed explanation of keywords and reserved words in MySQL 5.7

Preface The keywords of MySQL and Oracle are not ...

Introduction to the deletion process of B-tree

In the previous article https://www.jb51.net/arti...

The use of textarea in html and common problems and case analysis

The textarea tag is an HTML tag that we often use....

How to use mysql index merge

Index merging is an intelligent algorithm provide...

8 JS reduce usage examples and reduce operation methods

reduce method is an array iteration method. Unlik...

Interactive experience trends that will become mainstream in 2015-2016

The most important interactive design article in ...

The difference between MySQL database stored procedures and transactions

Transactions ensure the atomicity of multiple SQL...