按顺序插入表格

问题描述:

如何通过以下方式将值插入表(MySQL):
在表的所有行上,按ID列(PK)的顺序,在列'num'中插入递增编号?
例如,如果表有3行,其ID为1,5,2,我希望ID 1获得num = 1,ID 2获得num = 2,ID 5获得num = 3.

How can I insert values into a table (MySQL) in the following manner:
On all the rows of a table, in order of ID column (PK), insert incrementing number in column 'num'?
For example if the table had 3 rows , with Ids 1,5,2, I want ID 1 to get num=1, ID 2 to get num=2 and ID 5 to get num=3.

编辑 我将解释为什么我(认为我)需要这个:
我正在尝试将表中的列拆分为具有1对1关系的单独表.我以为我可以按ID的顺序获取所有值,然后将它们插入具有自动递增PK的新表中.那么我知道,按照ID的顺序,原始表中新引用列的值将是自动递增的数字.因此,我想按此顺序插入它们.我希望这很清楚.

EDIT I will explain why I (think I) need this:
I am trying to split a column off a table into a separate table with a 1-to-1 relation. I thought I would get all the values in order of ID and insert them into the new table, with an auto-incrementing PK. then I know that, in order of ID, the values for the new reference column in the original table will be auto-incrementing numbers. So I want to insert them in that order. I hope this is clear.

我找到了答案.很简单:

I found the answer. It is very simple:

SET @c=0;
UPDATE myTable SET num = (@c:=@c+1) ORDER BY id