简体   繁体   English

mysql正则表达式使用别名进行搜索

[英]mysql regexp for search using alias

I am not very good with regexp so I really would like some help to achieve my goal. 我对regexp不太满意,因此我真的希望获得一些帮助以实现我的目标。
When searching in my db I use an alias for specific keywords. 在数据库中搜索时,我为特定的关键字使用别名。

Here is an example 这是一个例子

keyword  tets alias test   (someone have spell wrong then word test)
keyword  b.m.w  alias bmw  (if someone write b.m.w instead of bmw)

etc. 等等

So far if a user searches for "bmw 316" I use LIKE "%bmw%316%" to get the results. 到目前为止,如果用户搜索"bmw 316"我将使用LIKE "%bmw%316%"来获得结果。 Now if the user searches for "bmw 316" I must use 现在,如果用户搜索"bmw 316" ,则必须使用

"%b.m.w%316%" OR
"%bmw%316%"

because bmw has alias bmw . 因为bmw具有别名bmw

In the case of 6 words with 2-3 aliases there are too many combinations. 如果6个单词的别名为2-3,则组合太多。
I am trying to achieve it with regexp. 我正在尝试使用正则表达式来实现它。
In the scenario above it would be something like (bmw|bmw) 316 . 在上述情况下,它将类似于(bmw|bmw) 316

How do I solve this problem? 我该如何解决这个问题?

You are not looking for REGEXP you are looking for a thing called levenshtein distance 您不是在寻找REGEXP而是在寻找一种叫做levenshtein distance的东西

MySQL does not (yet) have native support for this (wonderful) concept, but you can download a UDF here: MySQL还没有对此(出色的)概念提供本机支持,但是您可以在此处下载UDF:
http://joshdrew.com/ http://joshdrew.com/
And here's a list so you've got something to choose from: 这是一个列表,因此您可以从以下列表中进行选择:
http://blog.lolyco.com/sean/2008/08/27/damerau-levenshtein-algorithm-levenshtein-with-transpositions/ http://blog.lolyco.com/sean/2008/08/27/damerau-levenshtein-algorithm-levenshtein-with-transpositions/

You can also write your own function in MySQL, so you don't have to install a UDF. 您也可以在MySQL中编写自己的函数,因此不必安装UDF。
http://www.supermind.org/blog/927/working-mysql-5-1-levenshtein-stored-procedure http://www.supermind.org/blog/927/working-mysql-5-1-levenshtein-stored-procedure

Finally this question might help you out as well: 最后,这个问题也可能对您有所帮助:
Implementation of Levenshtein distance for mysql/fuzzy search? Levenshtein距离的实现为MySQL /模糊搜索?

A query for the closest match would look something like: 最匹配的查询看起来像:

SELECT * FROM atable a ORDER BY levenshtein(a.field, '$search') ASC LIMIT 10

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

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