简体   繁体   English

MYSQL + PHP删除表中的行时如何更新自动递增的ID?

[英]MYSQL+PHP How to update auto-incremented id when delete row in table?

 $q = "DELETE FROM newfiles WHERE id=$id LIMIT 1;";
 $q .= "SET @i=0;";
 $q .= "UPDATE newfiles SET id=(@i +:=1)";

Ex:- I have 6 Rows 例如:-我有6行

1 2 3 4 5 6 when i delete Row 4 i want the row 5 to become 4 and 6 to 5 1 2 3 4 5 6当我删除第4行时,我希望第5行成为4,第6至5

Generally this is not a good practice. 通常,这不是一个好习惯。 But you can try like this. 但是您可以尝试这样。 This will work. 这将起作用。

$q = "SET @id = $id;";
$q .= "DELETE FROM newfiles WHERE id = @id;";
$q .= "UPDATE newfiles SET id = id-1 WHERE id > @id;";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MySQL上使用PDO时存储自动递增ID - Storing Auto-Incremented ID when using PDO for MySQL 使用php插入值后,如何获取行的自动递增ID? - How to get the auto-incremented ID for a row once you insert values using php? 如何在php中插入mysql的自动递增字段ID,并将一个表的ID更新为另一个表? - How to insert the auto incremented field id of mysql in php and update the id of one table to another? 阅读下一个或上一个mysql行并更新当前行,而无需使用游标或自动递增的值 - Read next or previous mysql rows and update current row without using cursors or auto-incremented value 如何将一个表中的自动递增的ID链接到另一表中的ID? - How do I link the auto-incremented id from one table to the id in another table? 如何使用自动增加的列按存储过程将数据插入表 - How to insert data to table by store procedure with auto-incremented column 自动递增“id”列的问题 - Problem with auto-incremented “id” column MySQL奇数和偶数自动递增的ID - MySQL odd and even auto-incremented IDs 如何通过外部DIV ID访问内部div的ID(自动递增)? - How to access a internal div's ID (auto-incremented) by an external DIV id? 使用php和mysql自定义自动递增的“ id” - Custom Auto incremented “id” using php and mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM