sdgfsdh
sdgfsdh

Reputation: 37045

Postgres WHERE two arrays have a non-empty intersection

I have a table with a column tags of type varchar [].

I would like to select all rows where the tags contains at least one of a set of values.

Something like this:

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

What is the syntax for this?


I know I can do a series of ORs but that seems less elegant.

Upvotes: 11

Views: 5034

Answers (1)

GMB
GMB

Reputation: 222482

You can use &&, the array overlap operator:

select *
from foo
where tags && ARRAY['apples', 'bananas', 'cherries']

From the documentation:

&&: overlap (have elements in common)

Upvotes: 16

Related Questions