这三行代码必须同时执行。
SET @num := 0;
UPDATE your_table SET id = @num := (@num+1);
ALTER TABLE tableName AUTO_INCREMENT = 1;
********************************************************
Statement s1 = DataCon.getDataCon().createStatement();
s1.execute("SET @num := 0; UPDATE tblstaff SET staffid = @num := (@num+1); ALTER TABLE tblstaff AUTO_INCREMENT = 1");
s1.close();
答案 0 :(得分:1)
试试这个!如果你想按数字顺序保存数据,我发现它非常有用
Statement stm = DataCon.getDataCon().createStatement();
String stm1 = "SET @num := 0";
String stm2 = "UPDATE tblstaff SET staffid = @num := (@num+1)";
String stm3 = "ALTER TABLE tblstaff AUTO_INCREMENT = 1";
stm.addBatch(stm1);
stm.addBatch(stm2);
stm.addBatch(stm3);
stm.executeBatch();
stm.close();
答案 1 :(得分:0)
有趣的是,你并不需要。由于您未使用order by
,因此可以使用:
UPDATE your_table CROSS JOIN (SELECT @num := 0) params
SET id = @num := (@num+1);
话虽如此,ORDER BY
在这种情况下通常是可取的。