A solution to the problem of character set matching in MySQL Chinese query

Source: Internet
Author: User
Tags character set comparison mysql in mysql locate

My question when I write a query condition is as follows:

If I want to write all the records in a field that contain the word "Li"

? $str = "Lee";

SELECT * FROM table where field like '% $STR% ';

In addition to the records that contain the word "Li", there are records that do not contain the word "Li". Why?

In MySQL, the sorting and searching of Chinese characters are wrong when they are sorted and searched. This situation exists in many versions of MySQL. If this problem is not resolved, then MySQL will not be able to actually handle Chinese.

The reason for this problem is: MySQL in the query string is not sensitive to the case, when compiling the MySQL in general with the ISO-8859 character set as the default character set, so in the comparison process of Chinese encoded character case conversion caused this phenomenon.

Method One:

The workaround is to add the "binary" attribute to the field containing Chinese as a binary comparison, such as changing "name char (10)" to "name char" binary.

Method Two:

If you use the source code to compile MySQL, you can compile MySQL using the--WITH--CHARSET=GBK parameters, so that MySQL will directly support Chinese search and sorting.

Method Three:

You can use the Mysql locate function to determine. Take the above questions for example, using the following methods:

SELECT * FROM table WHERE locate (field, ' Lee ') > 0;

This site is the use of this method, it feels good. :P

Method Four:

Change your SELECT statement to this, select * from TABLE WHERE FIELDS like BINARY '%find% '!

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.