我想在MySQL数据库中搜索作者姓名。问题是同一作者姓名可以用各种方式拼写(或输入数据库)。我需要检查类似的名称。
我的authors
表的示例如下;
+---------+-------------+
| id | author |
+---------+-------------+
| 1 | JK Rowling |
+---------+-------------+
| 2 | J.K Rowling |
+---------+-------------+
| 3 | Rowling JK |
+---------+-------------+
| 4 | Rowling J.K |
+---------+-------------+
| 5 | JK. Rowling |
+---------+-------------+
| 6 | Jim Jones |
+---------+-------------+
我想找到J.K罗琳的所有书籍,所以我使用的查询是;
SELECT *
FROM `authors`
WHERE `author` LIKE '%J.K Rowling%'
返回no results
。
SELECT *
FROM `authors`
WHERE `author` LIKE 'J.K Rowling%'
返回no results
。
SELECT *
FROM `authors`
WHERE `author` LIKE '%J.K Rowling'
返回no results
。
如何构建查询以返回类似作者。
由于
答案 0 :(得分:0)
看起来类似的名字是罗琳,所以我应该试试这个。
SELECT *
FROM `authors`
WHERE `author` LIKE '%Rowling%'
答案 1 :(得分:0)
这是因为您使用%J.K Rowling%
,这意味着该流程只会搜索J.K Rowling
而且不会更少的行。根据您的数据对象,它上面没有J.K Rowling
。您可能想尝试Rowling
或类似的东西。
答案 2 :(得分:0)
您的问题是"。"
根据MySQL文档,使用LIKE运算符时唯一的特殊字符是"%" (百分比:匹配0,1或多个字符)和" _" (下划线:匹配一个且只匹配一个字符)。 http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
A"。" (句点)对MySQL的REGEXP运算符有特殊含义,但它仍应匹配列中的文字句点。 http://dev.mysql.com/doc/refman/5.0/en/regexp.html