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

Detailed explanation of fetch network request encapsulation example

export default ({ url, method = 'GET', da...

How to deploy zabbix_agent in docker

zabbix_agent deployment: Recommendation: zabbix_a...

Handwriting implementation of new in JS

Table of contents 1 Introduction to the new opera...

Example of how to configure cross-domain failure repair in nginx

Nginx cross-domain configuration does not take ef...

How to install and configure MySQL and change the root password

1. Installation apt-get install mysql-server requ...

A record of the pitfalls of the WeChat applet component life cycle

The component lifecycle is usually where our busi...

Steps to create a CentOS container through Docker

Table of contents Preface Create a bridge network...

Json string + Cookie + localstorage in JS

Table of contents 1.Json string 1.1Json Syntax 1....

Example of using CSS to achieve semi-transparent background and opaque text

This article introduces an example of how to use ...

Tutorial on installing MYSQL8.X on Centos

MySQL installation (4, 5, 6 can be omitted) State...

Detailed explanation of HTML area tag

The <area> tag defines an area in an image ...

svg+css or js to create tick animation effect

Previously, my boss asked me to make a program th...

How to solve the problem that the project in eclipse cannot be added to tomcat

1. Right-click the project and select properties ...

js to achieve cool fireworks effect

This article shares the specific code for using j...

Detailed explanation of MySQL slow queries

Query mysql operation information show status -- ...