Share the post "How To Swap Column Values Of 2 Rows In MySQL"
Setting the sort order between 2 rows in MySQL can be done in 1 line by making use of the IF() function.
I had a scenario where I had move up and move down functionalities to set the order of my database entries.
Let us say, you have columns ID and SORT_ORDER.
This line of code should do the trick.
|
1 |
UPDATE table SET sort_order = IF(sort_order = sortordervalue1, sortordervalue2, sortordervalue1) WHERE id IN(id1, id2)'; |