采集别人的数据后,希望将原顺序打乱,重新存放,有利于seo。原理是先将原数据库重新排序再插入新的数据库,既保证数据总量不会变化又实现id主键自增。
如原数据表oldtable如下:
id name
1 ts
2 ab
3 at
4 ax
...
目标有多个结果,举其一例如下:
id name
1 ab
2 ax
3 ts
4 at
法一:新建表,用insert
create table newtable like oldtable insert into newtable (id,name....) select (@a := @a+1) as id,t.name,t..... from (select name,.... from oldtable order by rand()) as t,(select @a := 0) as t1
法二:用update
UPDATE TABLE_NAME T SET ID = (SELECT RR FROM (SELECT ID,@R:=@R+1 RR FROM TABLE_NAME T,(SELECT @R:=0 ) R ORDER BY '这里写需要重新排序的字段') TT WHERE TT.ID=T.ID) ;