Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres array contains any from another array?

Tags:

postgresql

I am looking to perform a query that returns a row if the array column type contains at least one entry from another array, so example:

Row1

col_a = {1,2}

I want to a query that says col_a contains {3,1} and it should return Row1 because col_a contains '1'.

The value {3,1} in the example will come from another subquery. I am not sure this is possible, and I am struggling to find an example. The closest I found was the @> operator but that requires all entries to be in col_a right rather than any matching?

like image 736
iQ. Avatar asked Sep 06 '25 01:09

iQ.


1 Answers

You can use the && (overlap) operator

select array[1,2,3,4] && array[3,9];
 ?column?
----------
 t
(1 row)
like image 129
JGH Avatar answered Sep 07 '25 14:09

JGH