How to update a MySQL column with ascending numbers

Updated . Posted . Visible to the public.

Given the problem you have a new column postion and that column should be updated for all existing rows with ascending numbers. Furthermore these numbers should be generated by a special order. In order to achieve that you could do the following:

execute "SET @pos := 0;"
update " UPDATE pages SET position = ( SELECT @pos := @pos + 1 ) ORDER BY updated_at DESC;"
Profile picture of Ulrich Berkmüller
Ulrich Berkmüller
Last edit
Keywords
increase, counter, increment, migration
License
Source code in this card is licensed under the MIT License.
Posted by Ulrich Berkmüller to makandra dev (2011-10-12 07:40)