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

A little-known JS problem: [] == ![] is true, but {} == !{} is false

console.log( [] == ![] ) // true console.log( {} ...

Detailed explanation of overlay network in Docker

Translated from Docker official documentation, or...

Solution to the problem that input in form cannot be submitted when disabled

I wrote a test program before, in which adding and...

Vue+echarts realizes stacked bar chart

This article shares the specific code of Vue+echa...

Implementation of react automatic construction routing

Table of contents sequence 1. Centralized routing...

Why TypeScript's Enum is problematic

Table of contents What happened? When to use Cont...

Vue3 Vue CLI multi-environment configuration

Table of contents 1. Introduction 2. Switching 1....

javascript to switch pictures by clicking a button

This article example shares the specific code of ...

JS uses canvas technology to imitate echarts bar chart

Canvas is a new tag in HTML5. You can use js to o...

About the "occupational disease" of designers

I always feel that designers are the most sensiti...

HTML is actually the application of learning several important tags

After the article "This Will Be a Revolution&...

How to completely uninstall node and npm on mac

npm uninstall sudo npm uninstall npm -g If you en...

Teach you how to implement the observer mode in Javascript

Table of contents What is the Observer Pattern? S...

JavaScript uses promise to handle multiple repeated requests

1. Why write this article? You must have read a l...

XHTML introductory tutorial: Application of table tags

<br />Table is an awkward tag in XHTML, so y...