===============
| id | name |
===============
| 1 | Ali |
| 2 | ahmd |
| 3 | jada |
| 4 | nisr |
| 5 | sara |
| 6 | mona |
| 7 | dana |
===============
当我删除id = 5之类的任何行时,我想重新订购ID,就像那样=>
===============
| id | name |
===============
| 1 | Ali |
| 2 | ahmd |
| 3 | jada |
| 4 | nisr |
| 5 | mona |
| 6 | dana |
===============
如果我插入一个id = 2 name = yafa的行,如何重新排序表而不重复任何值,并且就像那样
| id | name |
===============
| 1 | Ali |
| 2 | yafa |
| 3 | ahmd |
| 4 | jada |
| 5 | nisr |
| 6 | sara |
| 7 | mona |
| 8 | dana |
===============
答案 0 :(得分:1)
这假设您始终知道要添加或删除的“id”。 (另外,您一次只能使用一行)。
删除:
DELETE FROM tableName WHERE id = 5;
UPDATE tableName
SET id = id-1
WHERE id > 5;
对于插入内容:
UPDATE tableName
SET id = id + 1
WHERE id >= 2;
INSERT INTO tableName (id, name) VALUES (2, 'yafa');
顺便问一下,你为什么要这样做呢?将列'id'设置为自动增量字段,让数据库处理它。如果你要跳过号码,你在乎吗?