d2kagw
d2kagw

Reputation: 807

PostgreSQL query for array in any array

I need a PostgreSQL command that matches from a list of emails against an array type column.

my column is declared as this:

emails character varying(255)[] DEFAULT '{}'::character varying[]

And I need to search against it using one of many potential matches. Normally I'd search using the IN operator like so: SELECT * FROM identities WHERE emails IN ['[email protected]']; but I can't seem to find an example of how to generate an IN query when searching against arrays.

Ideally it'd be something like this (which clearly doesn't work):

SELECT * FROM identities WHERE ('[email protected]','[email protected]') IN ANY (emails);

Upvotes: 1

Views: 138

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125534

The overlap && operator will check if there are elements in common

SELECT * 
FROM identities 
WHERE array['[email protected]','[email protected]']::varchar[] && emails;

http://www.postgresql.org/docs/current/static/functions-array.html#ARRAY-OPERATORS-TABLE

Upvotes: 3

Related Questions