开发者

Mysql regenerate missing IDs from 1 to N

I have a table with indexed column id2. After using this table inserting/deleting records, there are some missing records. id2 starts like this

1,2,3,4,8,9,13,55,66,67,68 .... 928746

How can I regenerate the sequence of id2 so it does not have missing numbers [1,2,3,4,5,6....]? Additionally, id2 order must be consistent with ts field, which is unix timestamp for record insertion.

Is it possible to do with mysql query, without any code (PHP开发者_JS百科)?

P.S Nothing relies on these IDS i can change them freely , i just want to do that . any hints ?


SET @rownum=0;
UPDATE [table] t, (SELECT @rownum:=@rownum+1 rownum, [table].* FROM [table]) r
SET t.[field] = r.rownum
WHERE (t.[primary_key] = r.[primary_key]) 

Replace [table] by your table name, [field] by your field name, and [primary_key] by the primary key column name

from : http://forums.mysql.com/read.php?61,402,108189

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜