You need to create a method that will accept item_id, sort. Assign this sort to the current item. And item whose sort> =: sort do sort + 1. Move the position by one.
The easiest option is through a loop. But can it somehow be done only on sql. sort = sort + 1 WHERE sort> =: sort AND item_id <>: item_id is not appropriate, since in this case the sort values will continue to grow indefinitely. And you need to grow in order from the provided sort value. Thank.
mysql
tbl products prod_id = 1 sort = 2 prod_id = 2 sort = 3 prod_id = 3 sort = 1 плохой вариант func sort($prod_id, $sort){ UPDATE products SET sort=$sort WHERE prod_id=$prod_id $prods = SELECT prod_id FROM products WHERE sort>=$sort foreach($prods as $prod){ $sort++ UPDATE products SET sort=$sort WHERE prod_id=$prod->prod_id } }
Select Id,@myId,@myId=id from sometable,@myId=0; Then at each iteration you will receive the current Id and previous ID - Vladimir Klykov