0

在 Postgres 中,如何将字符串与其他表中的列进行匹配?对于表 Istring列中的每一行,在表 II 和表 III 中的所有行中搜索匹配项并将它们/连接起来返回。

我的目标是从字符串输入中导出匹配词和匹配结构。

表一:字符串

string             | match_words | match_structures
---------------------------------------------------
Hi, my name is dan |             |

表二:单词

word
----------
hello
hi
bird
name

表三:结构

structure
----------
hi, my name is
hello, my
how are you?
my is

期望的输出:表 I:字符串

string             | match_words | match_structures
---------------------------------------------------
Hi, my name is dan | hi/name     | hi, my name is/my is

尝试:

使用 iLike: 我得到了第一个 match hi,但不是所有的子字符串都匹配:

update dialog set match_words = word from words where string ilike '%' || word || '%';

给我

string             | match_words | match_structures
---------------------------------------------------
Hi, my name is dan | hi          |

进行选择,我可以将完整的字符串与单个单词配对,我想这与我想要的相反:

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

       string       | word |   structure    
--------------------+------+----------------
 Hi, my name is dan | hi   | hi, my name is
 Hi, my name is dan | name | hi, my name is

这仍然缺少结构my ____ is

4

1 回答 1

0

首先,您必须使用一个子字符串,您需要获取表 1 并将字符串行的每个值除以字符串数组;第二个是对表 2 执行相同操作;第三个是比较您需要用作两个循环的表用数组 2 验证数组 1 的每个位置

https://www.postgresql.org/docs/8.1/static/functions-string.html https://w3resource.com/PostgreSQL/substring-function.php

于 2018-05-01T05:05:54.727 回答