As you can see in my SQL request I have a subquery which returns string array ( varchar[]
). I am trying to filter table_1
values by this array which I pass in ANY
command. For some reason I see this error:
ERROR: operator does not exist: character varying = character varying[] No operator matches the given name and argument types. You might need to add explicit type casts.
The data types of table_1.colomn_a
and table_2.colomn_a
columns are varchar
.
PostgreSQL version: 11.4
Where did I make a mistake?
select
table_1.*
from
table_1
where
table_1.colomn_a = any(
select
array_agg(table_2.colomn_a)
from
table_2
)
Of course, I can use such code, but I want to know the reason for the error in the first query:
select
table_1.*
from
table_1
where
table_1.colomn_a in(
select
table_2.colomn_a
from
table_2
)
Your subquery:
select
array_agg(table_2.colomn_a)
from
table_2
returns an array of an array of varchar, not an array of varchar. So you are trying to compare a varchar ( table_1.colomn_a
) with an array of varchar ( array_agg(table_2.colomn_a)
), which is not possible. Consider if your subquery had an explicit group by clause eg
select
array_agg(table_2.colomn_a)
from
table_2
group by table_2.colomn_b
In this case it's obvious that the query returns an array of arrays of varchar (one for each table_2.colomn_b
value). In your first query, there is an implicit group by
clause, which results in the output being an array of an array of varchar. You can just use:
select
table_2.colomn_a
from
table_2
and it will work fine (in this case = any
is equivalent to in
).
You already have an array. So use exists
:
where exists (select 1
from table_2 t2
where table_1.colomn_a = any(t2.colomn_a)
)
No need to bring the arrays together into a bigger array, just to parse that array to look for another value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.