【问题标题】:match a column of strings against columns in other tables将一列字符串与其他表中的列匹配
【发布时间】:2018-10-11 04:18:27
【问题描述】:

在 Postgres 中,如何将字符串与其他表中的列进行匹配?对于表 I string 列中的每一行,在表 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

所需的输出:表一:字符串

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的结构

【问题讨论】:

    标签: postgresql full-text-search psql tsvector


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 2013-06-18
      • 1970-01-01
      • 2020-09-09
      • 2018-09-13
      • 2021-08-02
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多