I need made a subquery with IN statement. Like this (simplify):
DELETE
FROM table_1
WHERE id_table_1 IN (SELECT string_agg(fk_id_table_1::TEXT,',') FROM table_2
I need made a massive delete, based on second subquery. The problem is if i use IN statement i must use string_agg to put the id separeted by commas, but id is integer and string_agg need change to text, if i put id_table_1::text IN (...subquery...) it return empty. I try using ANY too and put the id inside a integer array. But it show "ERROR: operator does not exist: integer = integer[]". I try use EXISTS too.
Somebody give me some light? Thanks a lot.
While you can code a literal values for a
INusing a comma delimited list, egWHERE id_table_1 IN (1,2,3), you can't usestring_aggin the way you're using it. Instead, use a query that returns a single column to create the list of values, like this:Better, use postgres's join delete syntax:
You could use
string_agg()if you were programatically building a query as a string, but that's not the situation here.