简体   繁体   English

MySql文本搜索找到类似的字符串

[英]MySql text search to find similar strings

I'm trying to find a way to query a particular database table column for similar strings or words. 我正在尝试找到一种方法来查询特定数据库表列中的类似字符串或单词。 For example, if I have a string 例如,如果我有一个字符串

I have a foo bar in my kitchen

The query would return results such as... 查询将返回诸如...之类的结果

foo fighters are a good band
raise the bar
kitchen remodeling contractors
foo bar is a funny word

Can this be done with one query? 这可以通过一个查询完成吗?

It seems like you are trying to match on any word in your given list. 您似乎想要匹配给定列表中的任何单词。 I would use the "OR" operator. 我会使用“OR”运算符。

Example (assuming your table is called my_table and the column you are searching is my_column ): 示例(假设您的表名为my_table ,您要搜索的列是my_column ):

SELECT * FROM my_table WHERE my_column like '%I%'
OR my_column LIKE '%have%'
OR my_column LIKE '%a%'
OR my_column LIKE '%foo%'
OR my_column LIKE '%bar%'
OR my_column LIKE '%in%'
OR my_column LIKE '%my%'
OR my_column LIKE '%kitchen%';

However, this query will run very slowly due to the double wildcards. 但是,由于双通配符,此查询将非常缓慢地运行。 You can also try http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html 您也可以尝试http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

At a basic level, you want to implement a full-text index on the column, so (assuming your column is col1 and your table is tbl), you'd need to alter table tbl add fulltext(col1); 在基本级别,您希望在列上实现全文索引 ,因此(假设您的列是col1而您的表是tbl),您需要alter table tbl add fulltext(col1); and search using select id from tbl where match(col1) against 'foo' or match(col1) against 'bar' or match(col1) against kitchen ... and so on, so forth, as described in this tutorial . 并使用select id from tbl where match(col1) against 'foo' or match(col1) against 'bar' or match(col1) against搜索select id from tbl where match(col1) against 'foo' or match(col1) against 'bar' or match(col1) against厨房...依此类推,如本教程所述 There's nothing wrong with @WinnieTong's answer, it's just that this sort of use case is precisely what a full-text index was designed for. @ WinnieTong的答案没有错,只是这种用例正是全文索引的设计目的。

When you get beyond the abilities of mysql , people generally use Sphinx or Apache SoLR to accomplish a searchable index. 当你超越了mysql的能力时,人们通常会使用SphinxApache SoLR来完成可搜索的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM