Postgres WHERE 两个数组有一个非空的交集

sdg*_*sdh 5 sql arrays postgresql where-clause

我有一个tags类型为 的列的表varchar []

我想选择标签至少包含一组值中的一个的所有行。

像这样的东西:

-- Not real code
SELECT * 
FROM foo 
WHERE non_empty(set_intersection(tags, '{ "apples", "bananas", "cherries" }'))
Run Code Online (Sandbox Code Playgroud)

这是什么语法?


我知道我可以做一系列的ORs 但这似乎不太优雅。

GMB*_*GMB 13

您可以使用&&,数组重叠运算符:

select *
from foo
where tags && ARRAY['apples', 'bananas', 'cherries']
Run Code Online (Sandbox Code Playgroud)

文档中:

&&:重叠(有共同元素)