开发者

Insert incremental numbers for all rows of a column

Long story but I'm dealing with a legacy ticket system where the 'id' field (pk/auto-increment) at one time was auto-incrementing by 1 but at some point the increment global variable was changed to 10. So I have a mess of tickets where the id for a lot of them is incremented by 1, but then starts incrementing by 10. I've been asked to cover this up.

I've created a new int field called "ticket_id" (not a pk and not auto-incrementing), and made some web-app开发者_Go百科 changes to read the last ticket_id number, add 1 and then save that with a new ticket record. I've changed the web app to use the new 'ticket_id' field on the surface (while really still using the old 'id' field behind the scenes).

For the existing ticket records - is there an easy way to quickly add in an incrementing value to the new column (first ordering by the id field)? 1..2..3..


Try something like the following:

$result=mysql_query("select * from my_table");

$counter =1;

while($row = mysql_fetch_assoc($result)){
 //suppose $row['id'] is your current primary key
 mysql_query("update my_table set ticker_id=$counter where id=".$row['id']);
$counter++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜