我有表格文章,其中id_post是主键和自动增量
id_post | post
1 hi1
3 hi3
27 hi27
45 hi45
67 hi67
69 hi69
我想更新id_post
以便使其在表中的行位置
id_post | post
1 hi1
2 hi3
3 hi27
4 hi45
5 hi67
6 hi69
怎么办?
我正在考虑使用ROW_NUMBER()
答案 0 :(得分:4)
运行以下命令:
SET @x = 0;
ALTER TABLE `posts` AUTO_INCREMENT = 1;
UPDATE `posts` SET `id_post` = @x:= @x + 1;
答案 1 :(得分:1)
您可以将set oprion用作波纹管:
before +--------+--------+ |id_post | itemID | +--------+--------+ | 1 | hi1 | | 3 | hi3 | | 27 | hi27 | | 45 | hi45 | | 67 | hi67 | | 69 | hi69 | +--------+--------+ after -------------------- mysql> SET @nbr=0; Query OK, 0 rows affected (0.00 sec) mysql> update posts set id=@nbr:=@nbr+1 +--------+--------+ |id_post | post | +--------+--------+ | 1 | hi1 | | 2 | hi3 | | 3 | hi27 | | 4 | hi45 | | 5 | hi67 | | 6 | hi69 | +--------+--------+ 6 rows in set (0.00 sec)