I want to combine two updates based on different condition into a single statement but getting an error and don’t understand any other way.
UPDATE "Main"
SET status = true
WHERE uid IN (
SELECT uid
FROM XYZ
)
UPDATE "Main"
SET status = false
WHERE uid NOT IN (
SELECT uid
FROM XYZ
)
I tried
UPDATE "Main"
SET status = CASE
WHEN uid IN (
SELECT uid
FROM XYZ) THEN true
ELSE false
END;
But getting error as
Unsafe query: ‘Update’ statement without ‘where’ updates all table rows at once
2
Answers
You can use the
LEFT JOIN
andCASE
clauses; the records that have a false status are those that are not matched :You can use a common table expression (CTE) to determine the values to set based on the condition and then use a single
UPDATE
statement with aJOIN
.