1

I am trying to remove entries from an array field that are found in a query.

tablename.listfield::integer[] has a full list I am trying to remove a list of values from that field, which are gathered within the update query. the ARRAY_REMOVE method only accepts single values, and the intarray module which has int[] - int[] doesn't seem to be an option.

the ARRAY[] && ARRAY[] can boolean return if there is overlap, but that doesn't help me

basically what I need is a real working version of this concept, which I know does not work.

UDPATE tablename SET listfield = ARRAY_REMOVE( listfield, ( select id from othertable ) )

is it possible to get this done with maybe a tricky CTE setup or something?

thanks!

1 Answer 1

2

I'm not sure why you say intarray doesn't seem to be an option, because it works just fine:

... SET listfield = listfield - ( SELECT array_agg(id) FROM othertable )

But if you want to do this without installing the extension, you can UNNEST the array and use the EXCEPT construct:

... SET listfield = ARRAY(SELECT UNNEST(listfield) EXCEPT SELECT id FROM othertable)
Sign up to request clarification or add additional context in comments.

1 Comment

fantastic thanks! looks like that's working exactly right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.