我一直在挣扎着试图找到解决方案几天。我正在尝试制作一个可以处理各种搜索术语的“智能”查询。查询运行正常,直到涉及到特殊字符,并且我在一些字符(如逗号和短划线)上使用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' ) );
有什么想法吗?
答案 0 :(得分:3)
你前进的方式是让你的数据库服务器一遍又一遍地做同样的工作,直到资源耗尽为止。
简短版本:
向表中添加另一列,其中包含已剥去特殊字符和低字符的标题。选择基于此。
中版:
使用Full-Text Indexing/Searching。
长版
创建一个单独的表结构,跟踪单个单词及其与各种标题的关联,并创建查询/程序逻辑以便用它进行搜索。
答案 1 :(得分:3)
您可以尝试这样做:
SELECT part_no, nomenclature FROM store_info WHERE
REPLACE(
REPLACE(
REPLACE(part_no,'/',''),'-',''),'.',''
) LIKE '%LWR0492%'