I have a table in mysql which I am trying to change the position of a row
|Id |Content |Position
------------------------------
|101 |"Yellow" |1
|201 |"Red" |2
|301 |"Blue" |3
|401 |"Black" |4
To be this
|Id |Content |Position
------------------------------
|101 |"Yellow" |2
|201 |"Red" |3
|301 |"Blue" |1
|401 |"Black" |4
I update this table using PHP the only data I have is the changed position e.g If I want to change the position of Yellow to 3 the only data I have is 3 so I want to be able to reorder everything under 3 to be 1,2 and everything over 3 to be 4,5.. The only data I have is to do this change is 3. how would I do this? Should it be done using PHP script or a mysql query? Which would be easier to implement?
I have tried
"UPDATE table SET position = 3 WHERE position = 2"
"UPDATE table SET position = 2 WHERE position = 3"
This will not use an ORDER BY in the statment as the positions basically swap. Prehaps I must create a tmp variable in mysql to store the old position?
I updated my api end point so I basically get the order from JavaScript and used the sql https://stackoverflow.com/a/15635201/4960941.
and it works.
I was using only position as a means to keep the ajax request as small as possible but this works for now. I'll try optimizing later when I have time.