查询以重新索引MySQL数据库的主键 [英] Query to Re-index Primary Key of MySQL Database

查看:77
本文介绍了查询以重新索引MySQL数据库的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL中有一个具有主键列的表.

I've got a table in MySQL that has a Primary Key Column.

让我们说:

ID | Value
1  | One
2  | Two
6  | Three
8  | Four
9  | Five

我如何做到这一点:

ID | Value
1  | One
2  | Two
3  | Three
4  | Four
5  | Five

没有其他表格.只是一个. 我只希望ID序列正确.

There are no other tables. Just the one. I just want the ID to be in a proper series.

有什么建议吗? 可能是一个查询..:)

Any suggestion?? A Query perhaps.. :)

推荐答案

甚至还有一种简单的方法可以通过编写此查询来完成结果

There is even a simple way to accomplish the result by writing this query

SET @newid=0;
UPDATE tablename SET primary_key_id=(@newid:=@newid+1) ORDER BY primary_key_id;

此查询将重新索引从1

这篇关于查询以重新索引MySQL数据库的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆