且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

模糊匹配SQL中的字符串

更新时间:2022-05-01 18:08:19

在postgres中,您可以使用 fuzzystrmatch 软件包。它提供了 levenshtein 函数,该函数返回两个文本之间的距离,然后可以使用以下示例性谓词执行模糊匹配:

In postgres you can use fuzzystrmatch package. It provies a levenshtein function, that returns distance between two texts, you can then perform fuzzy matching with the following exemplary predicate:

where levenshtein(street_address, '123 Main Avex') <= 1

这将匹配所有记录,因为'123 Main Ave'和'123 Main Avex'之间的距离是1(插入1个)。

This will match all records, because the distance between '123 Main Ave' and '123 Main Avex' is 1 (1 insertion).

Of当然,这里的值 1 只是一个例子,将非常严格地执行匹配(相差一个字符)。您应该使用更大的数字,或者使用@IVO GELOV表示的数字-使用相对距离(距离除以长度)。

Of course, value 1 here is just an example and will perform matching quite strictly (difference by only one character). You should either use larger number or, what @IVO GELOV sugests - use relative distance (distance divided by the length).