To set all Boys have an id starting from 10001 onwards and all Girls from 20001 in already existing IDs, use the following query. SET @i = 10000 ; UPDATE users SET uID = (@i := @i + 1) where gender =' b ' ORDER BY regDate ; The italic items can be changed as applicable If the uID already exists in this range, then first add a large value like 100000 to all the IDs so that duplicacy error does not appear.