SET @sort = 0; UPDATE t_tree AS T SET f_sort = (@sort := @sort +1) WHERE T.f_parent_id = 2 ORDER BY f_sort;