I have the simplified version of the table I have below. Each row has an item_order value partitioned by its parent_id.
| item_id | item_name | parent_id | item_order |
|---|---|---|---|
| 523 | fish | 1 | 1 |
| 562 | worm | 1 | 2 |
| 612 | mice | 1 | 3 |
| 251 | cheese | 1 | 4 |
| 723 | ketchup | 2 | 1 |
| 912 | pasta | 2 | 2 |
| 52 | chips | 2 | 3 |
Let's say that I want to set the 'item_order' value of 'mice' to 1.
UPDATE
my_table
SET
item_order = 1
WHERE item_id = 612;
I need the table to be updated as below. (Expected changes are highlighted.)
| item_id | item_name | parent_id | item_order |
|---|---|---|---|
| 612 | mice | 1 | 3 → 1 |
| 523 | fish | 1 | 1 → 2 |
| 562 | worm | 1 | 2 → 3 |
| 251 | cheese | 1 | 4 |
| 723 | ketchup | 2 | 1 |
| 912 | pasta | 2 | 2 |
| 52 | chips | 2 | 3 |
How do I update the item_order value of the all affected rows within the same parent_id?