mysql update a column with an int based on order

自作多情 提交于 2019-11-28 06:37:18
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 target
join
(
     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
Salman A

See answers to this question:

Updating column so that it contains the row position

SET @counter = 0;

UPDATE 
my_table
SET MoneyOrder = @counter := @counter + 1
ORDER BY Money;

SET @counter = 0;

UPDATE 
my_table
SET QuantityOrder = @counter := @counter + 1
ORDER BY Quantity;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!