0

for example,there are several items in a table

 id |name|info
 1  |Jim |Male
 2  |Rob |Male

the id of the table is auto_increment, and now i want to update Jim's row to the bottom of Rob ,and get the newest id, aka

 id |name|info
 2  |Rob |Male
 3  |Jim |Male

and get the id of (3), what's the sql expression?

4

2 回答 2

4

由于您无法在同一更新查询中选择 max(id),因此您必须将 max(id) 定义为变量。您需要的查询:

SET @max = (SELECT max(`id`) FROM `table` FOR UPDATE);
UPDATE `table` SET `id` = @max+1 WHERE `name` = "Jim";

编辑:您不应该更新 id,因为它是唯一标识符。如果你想使用排序方法,你应该添加一个整数列“位置”。

于 2013-06-28T08:38:40.193 回答
1

there is no update for autoincrement column you could delete Jims row and then insert it again and it will be id = 3

 DELETE FROM `table` where id = 1

then

 INSERT INTO `table` (name , info) VALUES ('jim' , 'male')
于 2013-06-28T08:47:17.537 回答