How to solve MySQL Chinese sorting errors

Source: Internet
Author: User
Tags mysql tutorial

How to solve MySQL Chinese sorting errors
/*

Method 1

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

Mysql tutorial 5.x supports defining character sets for a column

Method 2

Add the "binary" attribute to a field that contains Chinese characters for binary comparison. For example, change "name char (10)" to "name char (10) binary ".
If you use the source code tutorial to compile MySQL, you can use the -- with -- charset = gbk parameter when compiling MySQL, so that MySQL can directly support Chinese search and sorting (latin1 by default ). You can also use extra-charsets = gb2312 and gbk to add multiple character sets.

Next let's look at an instance.

1) test1:
Create table 'test1 '(
'Id' int (4) unsigned not null auto_increment,
'Name' varchar (20) character set utf8 default NULL,
Primary key ('id ')
);

Insert into 'test1' ('id', 'name') values ('1', 'deng www. bKjia. c0m ');
Insert into 'test1' ('id', 'name') values ('2', 'lil ');
Insert into 'test1' ('id', 'name') values ('3', 'jiang ');
Insert into 'test1' ('id', 'name') values ('4', 'hu ');

Select name from test1 order by name;

The result is incorrect. Now we use

The data in the test1 table is not sorted by Chinese pinyin, but the test2 query results using the gbk character set are satisfactory.

Select * from core_vender_info order by convert (vender_abbrev USING gbk) COLLATE gbk_chinese_ci

When querying, use the convert function to encode the character set gb2312 used for the queried data.

*/
?>

Related Article

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.