note 112686 added to function.mysql-insert-id
| From: | hoangvu4000 at gmail dot com | Date: | Fri, 12 Jul 2013 17:09:30 +0000 |
| Subject: | note 112686 added to function.mysql-insert-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195597@lists.php.net to get a copy of this message | ||
How to get ID of the last updated row in MySQL?
75
down vote
I've found an answer to this problem :)
by Pomyk
SET @update_id := 0;
UPDATE some_table SET row = 'value', id = (SELECT @update_id := id)
WHERE some_other_row = 'blah' LIMIT 1;
SELECT @update_id;
EDIT by aefxx
This technique can be further expanded to retrieve the ID of every row affected by an update
statement:
SET @uids := null;
UPDATE footable
SET foo = 'bar'
WHERE fooid > 5
AND ( SELECT @uids := CONCAT_WS(',', fooid, @uids) );
SELECT @uids;
This will return a string with all the IDs concatenated by a colon.
(questions: 1388025 form stackoverflow)
----
Server IP: 69.147.83.199
Probable Submitter: 113.172.89.209
----
Manual Page -- http://php.net/manual/en/function.mysql-insert-id.php
Edit -- https://master.php.net/note/edit/112686
Del: integrated -- https://master.php.net/note/delete/112686/integrated
Del: useless -- https://master.php.net/note/delete/112686/useless
Del: bad code -- https://master.php.net/note/delete/112686/bad+code
Del: spam -- https://master.php.net/note/delete/112686/spam
Del: non-english -- https://master.php.net/note/delete/112686/non-english
Del: in docs -- https://master.php.net/note/delete/112686/in+docs
Del: other reasons-- https://master.php.net/note/delete/112686
Reject -- https://master.php.net/note/reject/112686
Search -- https://master.php.net/manage/user-notes.php