If I have the following table & amp; Data allows us to use sort_index for sorting: create table 'foo' (`id`inT (11) tap etanement,` bar_id` INT 11) default tap, `sort_index` INT (11) default faucet, primary key (` ID`)); INSERT VALUES (1,1), (1,2), (1,3), (1,4), (2,1), (2, 2, 2) in `Foo` (` bar_id`, `sort_index`) 2), (2,3), (2,4), (2,5); I want to be able to do the following things in the most efficient way: Move any foo entry to a position (scanned by bar_id) Make sure that sort_index is always 1 indexed and no gap You should be able to move items in the beginning and list And the end of Rule # 2 should still be implemented This should be done in complete queries and Do not be as short as possible (as the set can be very large and individual UPDATE s is not ideal What do I do I am trying to make it clear that the table is empty, so we have the following data: id | bar_id | Sort_index 1 | 1 | 1 2 | 1 | 2 3 | 1 | 3 4 | 1 | 4 | 5 | 2 | 1 | |...