MySQL sorting by conventional sorting, custom sorting, and sorting by Chinese pinyin letters

MySQL sorting by conventional sorting, custom sorting, and sorting by Chinese pinyin letters

MySQL regular sorting, custom sorting, and sorting by Chinese pinyin letters. When writing actual SQL, we sometimes need to sort the conditional set.

Here are 3 commonly used sorting methods, mark them down

1. Conventional sorting ASC DESC

ASC Positive Sequence

DESC Flashback

-- No need to say more here

2. Custom sorting

Custom sorting is to sort according to the specific string (number) order you want.

Mainly use the function FIELD(str,str1,str2,str3,...)

MySQL custom sorting, str is compared with str1, str2, str3..., and output in the order of str1, str2, str3... If str is null or does not exist in str1, str2, str3..., the sequence is 0.

eg:

SELECT * FROM TEST ORDER BY FIELD(value,'test1','test2','test3','test4') ASC/DESC

eg2:

SELECT * FROM TEST WHERE VALUE IN('test1','test2','test3','test4') ORDER BY FIELD(value,'test1','test2','test3','test4') ASC/DESC -- ensure that only those that meet the conditions are sorted

3. Sort by Chinese Pinyin

If the table field uses GBK encoding, we can directly order by value, because GBK itself is sorted according to the pinyin letters ABCDEFGHIGK..., when the first digit is the same, the second digit will be compared, and so on. If the table field uses UTF-8 encoding, we usually encode it, so we can use MySQL's convert method to convert gbk for sorting.

eg:

SELECT * FROM TEST ORDER BY CONVERT(value USING GBK) ASC/DESC

The above is what I introduced to you about MySQL regular sorting, custom sorting and Chinese pinyin alphabet sorting. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Usage and precautions of Mysql row number() sorting function
  • A brief tutorial on sorting MySQL query results by value
  • Two ways to re-sort the auto-increment ID from 1 in MySQL
  • Sorting by multiple fields and problem solving in MySQL
  • Two methods to sort Chinese data in MySQL by pinyin
  • Summary of the differences between utf8_unicode_ci and utf8_general_ci in MySQL
  • Database query sorting using random sorting results example (Oracle/MySQL/MS SQL Server)
  • Detailed example analysis of mysql keyword relevance sorting method
  • MYSQL sorts the distance between two longitudes from near to far
  • How to make MySQL support Chinese sorting

<<:  Detailed explanation of Nginx reverse generation Mogilefs distributed storage example

>>:  Detailed explanation of the use of Vue.js draggable text box component

Recommend

HTML table tag tutorial (11): horizontal alignment attribute ALIGN

In the horizontal direction, you can set the alig...

How to configure Nginx to support ipv6 under Linux system

1. Check whether the existing nginx supports ipv6...

HTML+CSS+JavaScript to achieve list loop scrolling example code

Description: Set a timer to replace the content of...

Vue implements the function of calling the mobile phone camera and album

This article shares the specific code of Vue to a...

Linux swap partition (detailed explanation)

Table of contents linux 1. What is SWAP 2. What d...

MySQL replication advantages and principles explained in detail

Replication is to transfer the DDL and DML operat...

Detailed explanation of meta tags (the role of meta tags)

No matter how wonderful your personal website is,...

Mini Program to Implement Simple List Function

This article example shares the specific code of ...

Detailed explanation of MySQL 30 military rules

1. Basic Specifications (1) InnoDB storage engine...

Execute the shell or program inside the Docker container on the host

In order to avoid repeatedly entering the Docker ...

How to install Nginx in CentOS7 and configure automatic startup

1. Download the installation package from the off...

MySQL 5.7.20 zip installation tutorial

MySQL 5.7.20 zip installation, the specific conte...