Home > Software design >  Use integer array in IN condition PostgreSQL
Use integer array in IN condition PostgreSQL

Time:12-29

I have integer Array like {10,13,20}

I have to pass this array in IN condition like

SELECT count(*) FROM table WHERE id IN (10,20,30)

I tried:

SELECT count(*) FROM table WHERE id IN(array_to_string(_array,','))

but it's throwing the error casting operator does not exist: bigint = text

Thanks in advance

CodePudding user response:

you can use below query:

Demo

SELECT count(*) FROM test WHERE id = any(array[10,20,30]::int[]);

CodePudding user response:

Use string_to_array instead of array_to_string. Cast the resulting array to bigint[] as the type of id is bigint. Here it is, easy to be parameterized:

SELECT count(*) FROM users u
 WHERE id = any(string_to_array('10,20,30',',')::bigint[]);
  • Related