Let MySQL support Chinese inquiry

xiaoxiao2021-03-06  64

Q:

I am writing a query condition as follows: If I want to write all the records containing the "Li" word $ str = "李"; select * from table where field like '% $ STR%'; displayed record In addition to the record of "Li" word, there is also a record of "Li" word. why?

A:

In mysql, when you are sorted and lookup, the sorting of Chinese characters is wrong. This situation exists in many versions of MySQL. If this problem does not resolve, then mysql will not actually handle Chinese.

The reason for this problem is that mysql is not sensitive when querying strings, generally in the ISO-8859 character set as the default character set when finishing MySQL, so Chinese coding characters case-sensitive conversion This phenomenon.

method one:

The solution is to add "binary" attributes to the field containing Chinese, so as binary comparisons, for example, "Name Char (10)" is changed to "Name Char (10) binary".

Method Two:

If you use the source code to compile mysql, you can use the --with - charset = GBK parameters when you compile MYSQL, so mysql will directly support Chinese findings and sort.

Method 3:

You can use the mysql Locate function to determine. Take the above problem as an example, how to use: SELECT * from Table Where Locate (Field, 'Lee')> 0; Method 4:

Change your SELECT statement to this, select * from table where fields like binary '% FIND%'!

转载请注明原文地址:https://www.9cbs.com/read-85302.html

New Post(0)