首页 > 解决方案 > 连接的两个表上的子字符串匹配

问题描述

如何对一个子字符串匹配到另一个子字符串执行连接。我似乎只能ilike搜索其中一个,不能同时搜索子字符串。

给定表格:

对话

string             
-------------------
Hi, my name is dan

结构

structure
----------
his name is / my name is
hello, my / you are
how are you?

预期输出:

string              | structure
-------------------------------
Hi, my name is dan  | his name is / my name is

尝试:

两个ilike模糊匹配:

select string, structure from dialog left join structures on ('%' || string || '%' ilike '%' || structure || '%');

两个模糊ilike匹配OR

select string, structure from dialog left join structures on (string ilike '%' || structure || '%') or (structure ilike '%' || string || '%');

两个输出:

string              | structure
-------------------------------
Hi, my name is dan  |

标签: sqlpostgresqljoinsubstring

解决方案


如果结构实际上匹配,您可以使用正则表达式:

select string, structure
from dialog d left join
     structures s
     on string ~ replace(string, ' / ', '|');

当然,这不适用于示例数据,因为字符串实际上并不匹配。

这也表明您的结构实际上应该是正则表达式。


推荐阅读