MySQL like match query out incorrect Chinese solution

Source: Internet
Author: User

This article describes a solution that matches queries incorrectly when using a like statement in MySQL

MySQL like search when found, with

Select title from Tb_name where title like '%a% '

The result of the results in addition to include a name of the Chinese "new" also appear in the search results, which makes me want to find out how MySQL matches the pattern and rules in the end, and in the match when the regular expression is also very common!

This problem occurs because MySQL is case insensitive when querying strings, and when Yi MySQL is generally used as the default character set for the ISO-8859 character set, this behavior is caused by the Chinese encoding character-case conversion during comparison.

Workaround:

1. When building a table, add the "binary" attribute to the field containing the Chinese, making it a binary comparison, for example, "name char (10)" to "name Char (TEN) binary". But this is case-sensitive when you match the field of the table.

2. If you compile MySQL using the source code, you can use the--WITH--CHARSET=GBK parameter when compiling, so that MySQL supports Chinese search and sorting directly.

3. Use MySQL's locate function to judge. Such as:

SELECT * FROM table WHERE Locate (SUBSTR,STR) >0;

Locate () has two forms: locate (SUBSTR,STR), locate (Substr,str,pos). Returns the position of substr in Str if STR does not contain substr returns 0. This function is also case insensitive.

4. Use SQL statements like this:

SELECT * from TABLE WHERE fields like BINARY '%find% '

But this is case-sensitive as 1, and if you want to make a case-insensitive query, use upper or lower to convert.

5. Use binary and UCase functions and concat functions. UCase is English all convert uppercase, concat to concatenate strings. The new SQL statements are as follows:

Select Id,title,name from achech_com.news where binary UCase (title) like Concat ('% ', UCase (' a '), '% ')

Can also be written as

Select Id,title,name from achech_com.news where binary UCase (title) Like UCase ('%a% ')

The results of the search are satisfactory, but the speed may be slow for n milliseconds. Because matching with like and% can have a certain effect on efficiency.

MySQL like match query out incorrect Chinese solution

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.