In this table, there is a column called ‘position’. This column counts up from 0. (First row has position 0, second has 1 etc.)
Now, if the row with position 4, moves up, I want to set the row above (row with position 3) to the current position. (So position 3 becomes position 4.)
So for example:
I have:
id = 12, position = 2
id = 11, position = 3
id = 9, position = 4
id = 8, position = 5
After the update I want:
id = 12, position = 2
id = 11, position = 4
id = 9, position = 3
id = 8, position = 5
I want to use the id of the row, to update the position.
This is what I’ve got, but it doesn’t work on MySQL:
UPDATE message
SET position = position + 1
WHERE position = (SELECT position - 1 FROM message WHERE id = 11);
When I try to run it, it gives me this error: ‘SQL Error(1093): You can’t specify target table ‘message’ for update in FROM clause’.
How can I fix this?
EDIT – after UPDATE with sample data from OP:
I assume you know the ID (=11) and want the data to change as illustrated in your updated question… you need to update 2 rows to achieve that: