Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL : Use of ANY for multiple values

I am trying to use the ANY function of PostgreSQL to search the value from array interger type column.

My SQL:

SELECT
    *
FROM
    company_employee_contacts
WHERE
    corporate_complaint_type_ids = ANY(ARRAY[1,3]::integer[])

But it is giving me below error:

ERROR: operator does not exist: integer[] = integer

Can anyone tell me why I am getting this error while I am typecasting it?

like image 533
Harshal Avatar asked Oct 31 '25 10:10

Harshal


1 Answers

because corporate_complaint_type_ids is not integer, but rather array of integers... You can't:

select '{2,3,4}'::int[] = ANY(ARRAY[1,3]::integer[]);
ERROR:  operator does not exist: integer[] = integer
LINE 1: select '{2,3,4}'::int[] = ANY(ARRAY[1,3]::integer[]);

instead you can check if arrays overlap:

postgres@pond93# select '{2,3,4}'::int[] && ARRAY[1,3]::integer[];
 ?column?
----------
 t
(1 row)

or you can check one array value against ANY(array):

postgres@pond93# select ('{2,3,4}'::int[])[1] = ANY(ARRAY[1,3]::integer[]);
 ?column?
----------
 f
(1 row)
like image 88
Vao Tsun Avatar answered Nov 02 '25 02:11

Vao Tsun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!