PostgreSQL:检查数组中的每个项是否包含更大的字符串

时间:2015-11-04 17:25:55

标签: sql arrays postgresql sql-like

我在PostgreSQL中有一个字符串数组:

SELECT ARRAY['dog', 'cat', 'mouse'];

我有一个很大的段落:

Dogs and cats have a range of interactions. The natural instincts of each species lead towards antagonistic interactions, though individual animals can have non-aggressive relationships with each other, particularly under conditions where humans have socialized non-aggressive behaviors.

The generally aggressive interactions between the species have been noted in cultural expressions.

对于数组中的每个项目,我想检查它是否出现在我的大段落字符串中。我知道任何一个字符串,我都可以做到以下几点:

SELECT paragraph_text ILIKE '%dog%';

但有没有办法同时检查数组中的每个字符串(对于任意数量的数组元素)而不需要求助于plpgsql?

2 个答案:

答案 0 :(得分:2)

我相信你想要这样的东西(假设paragraph_text是名为table的表中的列):

SELECT
    paragraph_text,
    sub.word,
    paragraph_text ILIKE '%' || sub.word || '%' as is_word_in_text
FROM
    table1 CROSS JOIN (
        SELECT unnest(ARRAY['dog', 'cat', 'mouse']) as word
    ) as sub;

函数unnest(array)从数组值创建记录表。您可以CROSS JOIN执行此操作,这意味着table1中的所有行都与该不同表中的所有行组合。

如果paragraph_text是某种静态值(不是来自表格),您可以这样做:

SELECT
    paragraph_text,
    sub.word,
    paragraph_text ILIKE '%' || sub.word || '%' as is_word_in_text
FROM (
         SELECT unnest(ARRAY['dog', 'cat', 'mouse']) as word
     ) as sub;

答案 1 :(得分:1)

此解决方案仅适用于postgres 8.4及更高版本,因为早期版本无法获得动乱。

drop table if exists t;
create temp table t (col1 text, search_terms text[] );
insert into t values
   ('postgress is awesome', array['postgres', 'is', 'bad']), 
   ('i like open source', array['open', 'code', 'i']), 
   ('sql is easy', array['mysql']);

drop table if exists t1;
select *, unnest(search_terms) as search_term into temp t1 from t;

-- depending on how you like to do pattern matching. 
-- it will look for term not whole words
select *, position(search_term in col1) from t1;


-- This will match only whole words.
select *, string_to_array(col1, E' ')@>string_to_array(search_term, E' ') from t1;

基本上,您需要将search_terms数组展平为一列,然后将长字符串与每个搜索项匹配。