3

Suppose I have a table Car. This table has the following columns: id(uuid), status(text), color(text).

I have pushed all the Car ids that have a specific color into an array:

Suppose I have an array of the Car ids (the size of the array can change):

let car_ids_arr = ['id1', 'id2', ...'idn']

Now I need to update all rows (for example, set status to false) in Car where the id matches the ids in arr. I don't want to do this in a for loop and make several calls to the database, I rather do it all in one call.

The size of my array of ids can change , so I am not sure how to approach this... any pointers?

1 Answer 1

4

That's easily done with array operator any():

update car
set status = false
where id = any(array[1, 2, 3])

If id actually is of uuid datatype:

update car
set status = false
where id = any(array[...]::uuid[])
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting an error when issuing this query: ERROR: operator does not exist: uuid = text Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
@kris: you did not mention that we were dealing with uuids. Then follow the error hint and do cast. See my updated answer.
Also, what if I want to use my array variable car_ids_arr in the query instead of array[...]?

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.