How to fill in the “holes” in auto-increment fields?

后端 未结 7 896
时光说笑
时光说笑 2020-11-27 06:12

I\'ve read some posts about this but none cover this issue.

I guess its not possible, but I\'ll ask anyway.

I have a table with more than 50.000 registers. I

7条回答
  •  失恋的感觉
    2020-11-27 07:03

    You might wanna clean up gaps in a priority column. The way below will give an auto increment field for the priority. The extra left join on the same tabel will make sure it is added in the same order as (in this case) the priority

    SET @a:=0;
    REPLACE INTO footable
     (id,priority)
        (
        SELECT tbl2.id, @a 
        FROM footable as tbl
        LEFT JOIN footable as tbl2 ON tbl2.id = tbl.id  
        WHERE (select @a:=@a+1)
        ORDER BY tbl.priority
    )
    

提交回复
热议问题