如何重新编号主索引

2021-11-20 00:00:00 sql mysql

我有一个简单的 MySQL 表,主索引 (id) 没有一一编号(1、31、35、100 等).我希望它们被编号为 (1, 2, 3, 4).请告诉我怎么做.我还想指出,我知道操作可能带来的后果,但我只是想整理一下桌子.

I have got a simple MySQL table and primary index (id) is not numbered one by one (1, 31, 35, 100 etc.). I want them to be numbered like (1, 2, 3, 4). Please tell me how to do it. I would also like to point that I am aware of possible consequences of the operation, but I just want to tidy up the table.

推荐答案

我同意其他方法也可以,但我只是给出了不同的想法.这将不需要任何临时表创建要求::

I agree other methods will work but I'm just giving a different idea. This will do without any temp table creation requirements::

SET @i=0;
UPDATE table_name SET column_name=(@i:=@i+1);

相关文章