Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 联接的两个表上的子字符串匹配_Sql_Postgresql_Join_Substring - Fatal编程技术网

Sql 联接的两个表上的子字符串匹配

Sql 联接的两个表上的子字符串匹配,sql,postgresql,join,substring,Sql,Postgresql,Join,Substring,如何在与另一个子字符串匹配的子字符串上执行联接。我似乎只能搜索其中一个,而不能同时搜索两个 给定表格: 对话框 string ------------------- Hi, my name is dan 结构 structure ---------- his name is / my name is hello, my / you are how are you? 预期输出: string | structure -------------

如何在与另一个子字符串匹配的子字符串上执行联接。我似乎只能搜索其中一个,而不能同时搜索两个

给定表格:

对话框

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
尝试:

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
匹配:

select string, structure from dialog left join structures on (string ilike '%' || structure || '%') or (structure ilike '%' || string || '%');
两种输出:

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

首先执行笛卡尔乘积,并使用WHERE子句进行限制,以查看您可以期望的结果

select string, structure from dialog CROSS join structures WHERE string ilike '%' || structure || '%' AND structure ilike '%' || string || '%'
我认为您的左连接尝试与任何内容都不匹配,因为ILIKE语句的左侧有通配符。这些都是照字面理解的。另外,在连接中使用“AND”:您需要两个谓词都为true的连接。交叉连接适合这里,因为您非常严格地定义了where子句


左连接仅在您希望绝对获得“对话框”的位置使用,并且可以选择连接“结构”。在这种情况下,执行“完全连接”,这样您就可以确切地看到所进行的匹配类型。稍后,您可以决定进一步过滤所有内容,并将where子句谓词放入适当的join子句中。

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

select string, structure
from dialog d left join
     structures s
     on string ~ replace(string, ' / ', '|');
当然,这对示例数据不起作用,因为字符串实际上并不匹配


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

他的名字是/我的名字是
为了强调Tim的评论,“他的名字是”与“我的名字是”不匹配,所以您不能进行匹配。时期你需要更仔细地考虑你的数据和你想要完成的事情。