I have a query that gives a list of identifiers:
ID 2 3 4 5 6 25
ID
integer
.
I want to get this result in ARRAY
integers
:
ID 2,3,4,5,6,25
I wrote this query:
select string_agg(ID::text,',') from A where .....
I need to convert it to text, otherwise it will not work. string_agg
expect to receive (text,text)
this works great, as this result should subsequently be used in many places that expect ARRAY
integers.
I tried:
select ('{' || string_agg(ID::text,',') || '}')::integer[] from A WHERE ...
which gives: {2,3,4,5,6,25}
in type int4 integer[]
but this is not the right type ... I need the same type as ARRAY
.
eg SELECT ARRAY[4,5]
gives an array integer[]
in simple words I want the result of my query to work (for example):
select * from b where b.ID = ANY (FIRST QUERY RESULT) // aka: = ANY (ARRAY[2,3,4,5,6,25])
this does not work, since ANY expects an array and it does not work with a regular integer [], I get an error message:
ERROR: operator does not exist: integer = integer []
Note: the query result is part of the function and will be stored in a variable for later work. Do not send it to places where you bypass the problem, and suggest a solution that ARRAY
integers
will not give.
EDIT : why
select * from b where b.ID = ANY (array [4,5])
works. but
select * from b where b.ID = ANY(select array_agg(ID) from A where ..... )
does not work
select * from b where b.ID = ANY(select array_agg(4))
not working either
the error is still:
ERROR: operator does not exist: integer = integer []