[英]Populate an auto increment type ID on an existing MySQL Database?
I have an existing MySQL Database that is accessed with PHP. 我有一个现有的MySQL数据库,可以通过PHP访问。 There is about 15,000 records and I need to quickly go in and update a new Design Number
column that was just added. 大约有15,000条记录,我需要快速进入并更新刚刚添加的新“ Design Number
列。 I need to iterate the whole dataset and set the value for the first record as 00001, the next 00002, and keep incrementing by one until I get to the end. 我需要遍历整个数据集,并将第一个记录的值设置为00001,将下一个00002设置为1,直到结束为止。 I have code in place to make sure all new records will have this applied but I need to update the existing 15,000 records quickly. 我有代码来确保所有新记录都将应用此代码,但是我需要快速更新现有的15,000条记录。
Is there a quick way to do this in MySQL without building a PHP script to quickly populate all the old records? 有没有一种快速的方法可以在MySQL中执行此操作而无需构建PHP脚本来快速填充所有旧记录?
Keep in mind this is not a regular auto increment filed in MySQL but instead a custom implementation to mimic that functionality in SugarCRM 请记住,这不是MySQL中的常规自动增量,而是模仿SugarCRM中该功能的自定义实现
I need to iterate this table... 我需要迭代这张桌子...
SELECT designnumber_c FROM d1_designs_cstm
Then update this designnumber_c
column and increment by +1 for each record while also maintaining a 00001
0
padding 然后更新此designnumber_c
列并为每条记录增加+1,同时还要保持00001
0
填充
Something like this: 像这样:
mysql> SET @id := 0;
mysql> UPDATE d1_designs_cstm SET designnumber_c = LPAD(@id:=@id+1, 6, '0');
To add more on Answer by @Patrick Savalle 在@Patrick Savalle的 Answer上添加更多内容
You can have the new column definition as 您可以将新列定义为
designnumber_c int(6) unsigned zerofill not null
with this: 有了这个:
Example : 范例 :
mysql> SET @id := 0;
mysql> UPDATE d1_designs_cstm
-> SET designnumber_c = @id:=@id+1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.