I know that in PostgreSQL you can run a query like:
SELECT (1 = ANY('{1,3,4,7}'::int[])) AS result
to check if the right-hand array contains the element 1
. I was wondering if there is an easy way to check if the right-hand array contains any element from the left-hand array. Something like:
SELECT ('{2,3}'::int[] = ANY('{1,3,4,7}'::int[])) AS result
Is there an easy way to do this without iterating over the left-hand loop myself?
Sure, use the &&
array-overlaps operator:
SELECT ARRAY[1,2] && ARRAY[1,3,4,7];