2012-01-14 112 views
18

我在表格中添加了一个空列,现在我想在其行中插入连续数字。是否有可能使用SQL来完成它?在MySQL中插入连续数字

+0

具有行NUM更新它们,你可以找到在SO – 2012-01-14 12:40:54

回答

42

运行下面的查询在yourField列递增值:

SELECT @i:=0; 
UPDATE yourTable SET yourField = @i:[email protected]+1; 
+0

例子这正是我需要。谢谢。 – Rendicahya 2012-01-14 13:03:19

+3

上帝的母亲,那很快! :o – 2013-11-29 21:47:11

-1

试试这个auto increment 如果你想在你的表中的每个你做

create table WithAutoInc(somID int AUTO_INCREMENT,somName_ char(100) ,primary key(somID)); 

插入递增的数字现在插入你可以这样做

insert into WithAutoInc (somName_) values ('presley'); 

结果是

enter image description here

0

正如我在评论中说的,你可以用它的行号更新每一行,

这里是一个link如何计算rownum它的mysql。

改变措辞:

update player, 
     (select @rownum:[email protected]+1 ‘rank’, p.* 
     from player p, 
     (SELECT @rownum:=0) r 
     order by score desc) player1 
set thatColumn= rank 
where player.id = player1.id