如何将字符串与撇号相匹配?

时间:2017-01-23 15:27:06

标签: mysql match

我有一个带撇号的字符串。例如Mark's House。在我的网址中,它的名称为www.mydomain.com/Marks-House/

我想通过提供的名称获取数据库条目,在本例中为Marks-House。因此,MySQL无法知道ks之间的撇号(Mar k's)。

在我的数据库中,该条目将保存为带有撇号的Mark's House

我的查询:

SELECT 
id, title_de AS title, de AS wiki 
FROM pages 
WHERE MATCH (title_de) 
against ('+ Marks-House' in Boolean mode) 
OR title_de LIKE '%Marks-House' LIMIT 1

是否可以忽略数据库撇号等字符来匹配此条目,因为我的URI路由会自动为空格添加-并删除撇号?

2 个答案:

答案 0 :(得分:2)

当您执行'时,可以使用REPLACE()删除WHERE。例如;

mysql> select replace("Mark's house", "'", "") as new_value;
+-------------+
| new_value   |
+-------------+
| Marks house |
+-------------+
1 row in set

这会使您的查询如下;

SELECT 
id, title_de AS title, de AS wiki 
FROM pages 
WHERE replace(title_de, "'", "") = "Marks-House" LIMIT 1

答案 1 :(得分:1)

一种方式可以基于双重

SELECT 
id, title_de AS title, de AS wiki 
FROM pages 
WHERE MATCH (title_de) 
against ('+ Marks-House' in Boolean mode) 
OR title_de LIKE '%Marks%' and title_de like '%House%' LIMIT 1

或多%

SELECT 
id, title_de AS title, de AS wiki 
FROM pages 
WHERE MATCH (title_de) 
against ('+ Marks-House' in Boolean mode) 
OR title_de LIKE '%Marks%House' LIMIT 1

或者也是正则表达式

SELECT 
id, title_de AS title, de AS wiki 
FROM pages 
WHERE MATCH (title_de) 
against ('+ Marks-House' in Boolean mode) 
OR title_de REGEXP 'Marks.+House' LIMIT 1