0

たとえば、テーブルにいくつかのアイテムがあります

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

テーブルの ID は auto_increment で、ジムの行を Rob の一番下に更新し、最新の ID を取得したいと考えています。

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

(3)のidを取得すると、SQL式は?

4

2 に答える 2

4

Since you can't select the max(id) in a the same update query, you have to define the max(id) as a variable. The query you need :

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

EDIT : You are not supposed to update an id since it's an unique identifier. If you want to use sorting methods, you should add an integer column "position".

于 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 に答える