How to create a sql mysql query in order to number the records in the table in a prepared column ?

Desirable 1 request if it is real.

  • According to what order to insert numbers? - stckvrw
  • from 1 onwards - Georgeeeb
  • It is clear, I mean if the lines already have for example ID from 1 to N, then in what order should they be assigned other numbers? (and in general, why if the ID is already ready sequence numbers) - stckvrw
  • The table already has the data and id there is not the serial number of the addition - Georgeeeb
  • @Georgeeeb if you did not record the order number of the addition, now you will not know in which order the records were added. - Darth

2 answers 2

update table set field=@num:=@num+1 where 0 in(select @num:=0) 

The numbering will go from 1. If you wish, you can correct the starting value and the method of calculating the following

    It will be enough to put in advance the prepared auto_increment column when creating it and fill it with numbers in order:

     ALTER TABLE таблица ADD COLUMN ( `заранее подготовленная колонка` int key auto_increment ); 

    or

     ALTER TABLE таблица CHANGE COLUMN `заранее подготовленная колонка` `заранее подготовленная колонка` int key auto_increment; 
    • If the table already has data? - Georgeeeb
    • Nothing bad will happen. If there is no other auto_increment column in the table, then everything will go according to plan - a column filled with numbers will be created (or an empty column will be filled with numbers). And if it is - it is not clear why the vehicle needed a second one .. The request will simply generate an error. - Darth