mysql update a column with an int based on order mysql update a column with an int based on order sql sql

mysql update a column with an int based on order


SET @rownumber = 0;    update mytable set Moneyorder = (@rownumber:=@rownumber+1)order by MoneyOrder asc

or to do it in a single query you can try

update mytable targetjoin(     select id, (@rownumber := @rownumber + 1) as rownum     from mytable              cross join (select @rownumber := 0) r     order by MoneyOrder asc) source on target.id = source.id    set Moneyorder = rownum


See answers to this question:

Updating column so that it contains the row position

SET @counter = 0;UPDATE my_tableSET MoneyOrder = @counter := @counter + 1ORDER BY Money;SET @counter = 0;UPDATE my_tableSET QuantityOrder = @counter := @counter + 1ORDER BY Quantity;