Mysql Chinese sorting rules description

Mysql Chinese sorting rules description

When using MySQL, we often sort and query a field. We usually want to sort by the first letter of the Chinese pinyin. However, when sorting Chinese characters in MySQL, the sorting results of Chinese characters are often wrong.

This situation exists in many versions of MySQL.

If this problem is not solved, MySQL will not be able to actually process Chinese.

The reason for this problem is that MySQL is case-insensitive when querying strings. When compiling MySQL, the ISO-8859 character set is generally used as the default character set. Therefore, the case conversion of Chinese encoded characters during the comparison process causes this phenomenon.

After checking the information, there are two solutions:

1. Add the "binary" attribute to the field containing Chinese characters so that it can be compared as a binary field. For example, change "name varchar(10)" to "name varchar(10)binary".

2. If you do not want to modify the table structure or recompile MySQL, you can also use the CONVERT function in the order by part of the query statement.

For example, if the name field is in Chinese and needs to be sorted, you can write select * from mytable order by CONVERT(name USING gbk);

Supplement: MySQL database default sorting problem

1. MySQL official answer:

SELECT * FROM tbl -- this will do a "table scan". If the table has never had any DELETEs/REPLACEs/UPDATEs, the records will happen to be in the insertion order, hence what you observed.

This means that if a MyISAM engine table is not deleted or modified, and a select statement without order by is executed, the table will be sorted in the order of insertion.

If you had done the same statement with an InnoDB table, they would have been delivered in PRIMARY KEY order, not INSERT order. Again, this is an artifact of the underlying implementation, not something to depend on.

For InnoDB engine tables, under the same circumstances, select without order by will sort by primary key, from small to large.

2. View the database engine command:

(1) View the engine used by a table

show create table;

(2) Check which engines MySQL supports

show engines;

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Example of utf8mb4 collation in MySQL
  • MySQL aggregate function sorting
  • MySQL sorting using index scan
  • Some lesser-known sorting methods in MySQL
  • 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

<<:  Vue Page Stack Manager Details

>>:  HTML Tutorial: Collection of commonly used HTML tags (5)

Recommend

Solution to the problem that Docker container cannot be stopped or killed

Docker version 1.13.1 Problem Process A MySQL con...

Analyze several common solutions to MySQL exceptions

Table of contents Preface 1. The database name or...

Detailed steps for porting busybox to build a minimal root file system

Busybox: A Swiss Army knife filled with small com...

How to use the Linux md5sum command

01. Command Overview md5sum - Calculate and verif...

How to automatically execute SQL statements when MySQL in Docker starts

When creating a MySQL container with Docker, some...

Detailed tutorial on installing Docker on Windows

Since my local MySQL version is relatively low, I...

JavaScript+HTML to implement student information management system

Table of contents 1. Introduction 2. Rendering 3....

Detailed explanation of CocosCreator message distribution mechanism

Overview This article begins to introduce content...

JS Asynchronous Stack Tracing: Why await is better than Promise

Overview The fundamental difference between async...

Solve the conflict between docker and vmware

1. Docker startup problem: Problem Solved: You ne...

How to start a Vue.js project

Table of contents 1. Node.js and Vue 2. Run the f...

Windows 10 is too difficult to use. How to customize your Ubuntu?

Author | Editor Awen | Produced by Tu Min | CSDN ...

Automatically install the Linux system based on cobbler

1. Install components yum install epel-rpm-macros...

JavaScript implements draggable modal box

This article shares the specific code of JavaScri...