Luke Schlangen
Luke Schlangen

Reputation: 3882

PostgreSQL UPDATE all rows in an array to something while updating all rows NOT in array to something else

I have a query that seems to work:

UPDATE questions SET active=TRUE WHERE id=ANY ([1, 5, 7]);

But I also want to set all other rows to active=FALSE

Essentially, I want to do the equivalent of running these two lines

UPDATE questions SET active=FALSE;
UPDATE questions SET active=TRUE WHERE id=ANY ([1, 5, 7]);

But I would like to accomplish this in a single query if possible. Is there a way to do this?

Upvotes: 0

Views: 59

Answers (1)

klin
klin

Reputation: 121624

Use the condition as a Boolean value to set:

UPDATE questions SET active = (id = ANY(array[1, 5, 7]));

Upvotes: 2

Related Questions