这几天我一直在绞尽脑汁寻找解决方案。我正在尝试制作一个可以处理范围广泛的搜索词的“智能”查询。在涉及特殊字符之前,查询运行良好,并且我在某些字符(如逗号和破折号)上使用 REPLACE 方法取得了一些成功。其他字符(例如引号和符号)将导致空查询。
举几个例子:
我要搜索的原始名称是“French Is Fun, Book 1 - 1 Year Option”,通过下面的查询,我得到了包含这些搜索词的返回结果:
1. "French Is Fun"
2. "French Is Fun, book"
3. "French Is Fun, book"
4. "French Is Fun, Book 1"
SELECT * FROM `products` WHERE ( (LOWER(name) LIKE '%french is fun book%' OR
LOWER(replace(name, ' ','')) LIKE '%french is fun book%' OR
LOWER(replace(name, ' ','')) LIKE '%french is fun book%' OR
LOWER(replace(name, '-','')) LIKE '%french is fun book%')
但是,当原始标题中有这样一个符号时:“全局历史与地理:文明的发展 - 1 年选项”- 当我尝试这些不同的搜索词时,我得到一个空查询:
1. "Global History & Geography"
2. "Global History Geography"
我试过了没用
SELECT * FROM `products` WHERE
(LOWER(name) LIKE '%global history geograph%' OR
LOWER(replace(name, ' ','')) LIKE '%global history geography%' OR
LOWER(replace(name, ' ','')) LIKE '%global history geography%' OR
LOWER(replace(name, ',','')) LIKE '%global history geography%' OR
LOWER(replace(name, '&','')) LIKE '%global history geography%' OR
LOWER(replace(name, '-','')) LIKE '%global history geography%');
我也试过在 & 符号中添加一个转义字符,但没有用:
SELECT * FROM `products` WHERE
(LOWER(name) LIKE '%global history geography%' OR
LOWER(replace(name, ' ','')) LIKE '%global history geography%' OR
LOWER(replace(name, ' ','')) LIKE '%global history geography%' OR
LOWER(replace(name, ',','')) LIKE '%global history geography%' OR
LOWER(replace(name, '\&','')) LIKE '%global history geography%' OR
LOWER(replace(name, '-','')) LIKE '%global history geography%');
名称中的逗号也返回空结果。作为演示,原名是这样的:
“Amsco 的 AP 微积分 AB/BC 为大学先修考试做准备 - 1 年选项”
此尝试总是返回空查询:
SELECT * FROM `products` WHERE
( (LOWER(name) LIKE '%amscos ap calculus%' OR
LOWER(replace(name, ' ','')) LIKE '%amscos ap calculus%' OR
LOWER(replace(name, '\'','')) LIKE '%amscos ap calculus%' OR
LOWER(replace(name, ',','')) LIKE '%amscos ap calculus%' OR
LOWER(replace(name, '-','')) LIKE '%amscos ap calculus%')
) AND ( (`products`.`type` = 'Rental' ) );
有什么想法吗?
最佳答案
按照您的方式,您将让您的数据库服务器一遍又一遍地执行相同的工作,直到它因资源耗尽而死掉。
简短版本:
向表中添加另一列,其中包含已去除特殊字符和小写的标题。以此为基础进行选择。
中等版本:
使用Full-Text Indexing/Searching .
长版
创建一个单独的表结构来跟踪单个单词及其与各种标题的关联,并创建查询/程序逻辑以使用它进行搜索。
关于mysql - 忽略搜索查询中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13072780/