I have a single table table1 where I am trying to update one column completed to certain values based on id. Because the table remains the same as well as the updated column and WHERE clause field, I have been looking into the potential of consolidating the SQL statements.
I have a table1 like this:
| id | completed |
| -- | --------- |
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
| 5 | NULL |
| 6 | NULL |
| 7 | NULL |
| 8 | NULL |
| 9 | NULL |
| 10 | NULL |
| 11 | NULL |
| 12 | NULL |
Here is an example of what I currently have as standard: (it continues on for another 13 UPDATE statements)
UPDATE table1 SET completed = 25
WHERE id IN (1, 2, 3, 4);
UPDATE table1 SET completed = 27
WHERE id IN (5, 6, 7, 8);
UPDATE table1 SET completed = 28
WHERE id IN (9, 10, 11, 12);
I have tried something like: (though I know it's not right)
UPDATE table1 (
SET completed = 25 WHERE id IN (1, 2, 3, 4),
SET completed = 27 WHERE id IN (5, 6, 7, 8),
SET completed = 28 WHERE id IN (9, 10, 11, 12)
);
ASSUMPTIONS: id is unordered in the real table; skips and does not correlate to completed number order
The expected result of table1:
| id | completed |
| -- | --------- |
| 1 | 25 |
| 2 | 25 |
| 3 | 25 |
| 4 | 25 |
| 5 | 26 |
| 6 | 26 |
| 7 | 26 |
| 8 | 26 |
| 9 | 27 |
| 10 | 27 |
| 11 | 27 |
| 12 | 27 |
QUESTION: Would there be an even more consolidated version given the above? Or is the first code the most efficient?
I have also looked around on stackoverflow and documentation but did not see something like this addressed directly.