简体   繁体   English

PostgreSQL将字符串匹配为动态输入的字符串

[英]PostgreSQL match string to dynamically entered string

I have a varchar field in my database table A let's call it store_name, this field gets its value from entity A, now entity B enters store_name into a different database table B now I want to get all records in table A where the store_name matches the values in table B. 我的数据库表A中有一个varchar字段,我们称它为store_name,此字段从实体A获取其值,现在实体B将store_name输入到另一个数据库表B中,现在我想获取表A中与store_name匹配的所有记录表B中的值。

How would you recommend me doing the query as I don't control the values of those 2 fields? 由于我不控制这两个字段的值,您如何建议我进行查询?

What do you think about PostgreSQL fuzzystrmatch? 您如何看待PostgreSQL Fuzzystrmatch? The tables contain thousands of records. 该表包含数千条记录。

Thanks 谢谢

Assuming that both table A and table B are in the same database. 假设表A和表B都在同一数据库中。 And I guess since you don't control insertion of data, you are not sure if the values are of same case or there may be a spelling mismatch. 而且我猜想是因为您不控制数据的插入,所以您不确定这些值是否大小写相同或可能存在拼写不匹配的情况。

Case 1: If the problem is only of case-mismatch, you can use ilike : 情况1:如果问题仅在于大小写不匹配,则可以使用ilike

Select a.store_name
from a, b
Where a.store_name ilike b.store_name

Case 2: If you also want to check for spelling mismatch, but words sound similar, then after installing postgresql-contrib package and creating extension fuzzystrmatch, you can use: 情况2:如果您还想检查拼写不匹配,但单词听起来相似,则在安装postgresql-contrib软件包并创建扩展名Fuzzystrmatch后,可以使用:

Select a.store_name
from a, b
Where a.store_name ilike b.store_name OR
soundex(a.store_name) = soundex(b.store_name)

If you are dealing with names, which may not always be in English, it may be more appropriate to use metaphone or dmetaphone function instead of soundex. 如果您使用的名称可能并不总是使用英文,则使用元音素或dmetaphone功能代替soundex可能更合适。

Documentation: Fuzzystrmatch 文档: Fuzzystrmatch

If you want matching you can use a straight up join. 如果要匹配,可以使用直接联接。

Select a.store_name
from a 
     join b on a.store_name = b.store_name; 

If you want to use fuzzy matching just use the various functions available in the join criteria. 如果要使用模糊匹配,只需使用联接条件中可用的各种功能。 Documentation here 这里的文件

Note: there are some limitations to Fuzzy string matching so i would advise testing each out on values that you either know match or don't. 注意:模糊字符串匹配有一些限制,所以我建议您对您知道匹配或不匹配的值进行测试。

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

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