将一列字符串与其他表

时间:2018-05-01 04:03:35

标签: postgresql full-text-search psql tsvector

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

我的目标是从字符串输入中派生匹配单词和匹配结构。

表I:字符串

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

表二:

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

表III:结构

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:我得到第一个匹配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

1 个答案:

答案 0 :(得分:0)

首先你必须使用子字符串 你需要取表1并划分字符串行的每个值  在字符串数组中 第二是对表2做同样的事情 第三是比较你的表 您将需要使用两个循环来验证数组1与阵列2的每个位置

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