MySQL中文模糊检索问题的解决方法
mysql|解决|问题|中文
MySQL中文模糊检索问题的解决方法
来源:林兴陆
在 MySQL 下,在进行中文模糊检索时,经常会返回一些与之不相关的
记录,如查找 "-0x1.ebea4bfbffaacp-4%" 时,返回的可能有中文字符,却没有 a 字符存在。
本人以前也曾遇到过类似问题,经详细阅读 MySQL 的 Manual ,发现可以
有一种方法很方便的解决并得到满意的结果。
例子:
·希望通过“标题”对新闻库进行检索,关键字可能包含是中英文,如
下 SQL 语句:
select id,title,name from achech_com.news where title like '-0x1.ebea4bfbebe84p-4%'
返回的结果,某些 title 字段确定带了“a”关键字,而有些则只有中文,
但也随之返回在检索结果中。
解决方法,使用 BINARY 属性进行检索,如:
select id,title,name from achech_com.news where binary title like '-0x1.ebea4bfbebe84p-4%'
返回的结果较之前正确,但英文字母区分大小写,故有时在检索如“Achech”
及“achech”的结果是不一样的。
知道了使用 BINARY 属性可以解决前面这个问题,再看看 MySQL 支持的
UCASE 及 CONCAT 函数,其中 UCASE 是将英文全部转成大写,而 CONCAT 函
数的作用是对字符进行连接,以下是我们完全解决后的 SQL 语句:
select id,title,name from achech_com.news where binary ucase(title) like concat('%',ucase('a'),'%')
检索的步骤是先将属性指定为 BINARY ,以精确检索结果,而被 like 的 title
内容存在大小写字母的可能,故先使用 ucase 函数将字段内容全部转换成大
写字母,然后再进行 like 操作,而 like 的操作使用模糊方法,使用 concat
的好处是传进来的可以是直接的关键字,不需要带“%”万用符,将“'a'”直接
换成你的变量,在任何语言下都万事无忧了。
当然你也可以这么写:
select id,title,name from achech_com.news where binary ucase(title) like ucase('0x0.00020bfbebe08p-1022%')
检索的结果还算满意吧,不过速度可能会因此而慢N毫秒喔。
相关资料:
Relate:
20.16 Case Sensitivity in Searches
By default, MySQL searches are case-insensitive (although there are some character sets that are never case insensitive, such as czech). That means that if you search with col_name LIKE 'a%', you will get all column values that start with A or a. If you want to make this search case-sensitive, use something like INDEX(col_name, "A")=0 to check a prefix. Or use STRCMP(col_name, "A") = 0 if the column value must be exactly "A".

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Title is the meaning that defines the title of the web page. It is located within the tag and is the text displayed in the title bar of the browser. Title is very important for the search engine optimization and user experience of the web page. When writing HTML web pages, you should pay attention to using relevant keywords and attractive descriptions to define the title element to attract more users to click and browse.

The title in HTML displays the title tag of the web page, which allows the viewer to know what the current page is about, so each web page should have a separate title.

1. The difference in matching content. LIKE requires that the entire data must be matched. With Like, all the contents of this field must meet the conditions; REGEXP only requires partial matching, and only one fragment needs to be satisfied. 2. Differences in matching positions: LIKE matches the entire column. If the matched text appears in the column value, LIKE will not find it and the corresponding row will not be returned (unless wildcards are used); REGEXP is within the column value. A match is performed. If the matched text appears in the column value, REGEXP will find it, the corresponding row will be returned, and REGEXP can match the entire column value (same effect as LIKE). 3. The SQL statement returns data that differs from LIKE matching: the SQL statement

The LIKE operator is used to search for a specified pattern in a column in the WHERE clause. Syntax: SELECTcolumn_name(s)FROMtable_nameWHEREcolumn_nameLIKEpatternpattern This is where the specified template is placed, and "%" is used here, also called the wildcard character %. If it is placed in front of the condition, it will search for data ending with...; for example: % If Li % is placed after the condition, then the data starting with... is searched; for example: Li %% exists before and after the condition, then the included data is searched; for example: % Li % Little knowledge point: ERROR1064 (42000):Youhaveane

Optimize the Like statement with Union 1) Sometimes, you may need to use the or operator for comparison in the query. When the or keyword is used too frequently in the where clause, it may cause the MySQL optimizer to mistakenly choose a full table scan to retrieve records. The union clause can make queries execute faster, especially when one of the queries has an optimized index and the other query also has an optimized index. For example, when there are indexes on first_name and last_name respectively, execute the following query statement: mysql>select*fromstudentswherefirst_namelike'A

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

图片消失如何解决先是图片文件上传$file=$_FILES['userfile']; if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'
