Utf8 Chinese sorting solution in MySQL

Source: Internet
Author: User

In MySQL, the default Character Set utf8 is used. The sorting order of Chinese characters by Pinyin is messy, not the result we want.

The solution is as follows:

1. If you do not want to change the table definition and default encoding, convert the fields to gbk encoding before sorting:

SELECT * FROM table order by convert (chinese_field USING gbk );

The premise is that the gbk character set is installed when MySQL is installed. Otherwise, an error is returned:

#1115-Unknown character set: 'gbk'

Add gbk encoding when compiling the source code. If the source code has been installed, re-compile and install it. Re-compile and install it will not affect the existing settings of MySQL, including data.

2. Change the character set of the field to gbk and use order by to sort the field directly.

MySQL 5.x supports defining character sets for a column.

3. Some people on the Internet say that adding the field definition to the binary Attribute can also achieve the effect. They did not perform tests and did not dare to comment.

Character Set verification rules are also mentioned in MySQL documents:

SELECT * FROM t1 order by a COLLATE utf8_bin;

However, this only affects the collation and does not affect the character set itself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.