Posted by blogmeister on
March 6, 2013
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.
UPDATE table SET sort_order = IF(sort_order = sortordervalue1, sortordervalue2, sortordervalue1) WHERE id IN(id1, id2)';