たとえば、テーブルにいくつかのアイテムがあります
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式は?
たとえば、テーブルにいくつかのアイテムがあります
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式は?
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".
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')