Query to replace current IDs with new
I imported a database with PHPMyAdmin. A lot of rows were useless and I ended up with 300 rows that get to the ID 500开发者_JAVA百科0 or more. How can I delete all current IDs and give them all a new IDs?
I have a column named Date
with timestamp. It would be good to order the IDs by date but it's not a big deal.
How would the query be?
To re-number all rows to have IDs from 1-n, try this:
First, run this to prevent the second query causing id collisions:
update mytable set id = id + 1000000;
Then run this, which will re-number all ids 1-n in order of Date:
set @id:=0;
update mytable set
id = (@id := @id + 1)
order by Date;
精彩评论