问题描述
我的数据库中有一个名称表,我希望对其进行模糊搜索,例如我的数据库包含:
I have a names table in my database and I would wish to conduct a fuzzy search on it for example my database contains:
Name ID
John Smith 1
Edward Smith 2
Gabriel Gray 3
Paul Roberts 4
当我通过python搜索数据库时,我只能进行精确匹配搜索.但是我希望能够进行模糊搜索,在其中我可以搜索名称史密斯",并带回约翰·史密斯和爱德华·史密斯.
At the moment when I search the database via python I can only do exact match searching. But I would like to be able to do fuzzy searching where by I can search for the name "smith" and bring back John Smith and Edward Smith.
推荐答案
以最简单的形式,您将使用LIKE
比较:
In simplest form, you'd use the LIKE
comparison:
SELECT * FROM table WHERE name LIKE '%smith%';
可以使用FULLTEXT索引(大量文本),SOUNDEX()
(适用于英语单词,适用于其他语言,包括从有些可行"到糟糕"的所有内容),levenshtein距离来完成更详尽的搜索等等.
More elaborate searches can de done with FULLTEXT index (large amounts of text), SOUNDEX()
(works on words in the english language, matching on other languages is everything from 'somewhat workable' to 'terrible'), levenshtein distance of words etc.
这篇关于MySQL:进行基本搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!