How to get the ID of the last updated row in MySQL?

Spread the love

Sometimes, we want to get the ID of the last updated row in MySQL.

In this article, we’ll look at how to get the ID of the last updated row in MySQL.

How to get the ID of the last updated row in MySQL?

To get the ID of the last updated row in MySQL, we can define a variable that has the ID of the entry we’re updating.

For instance, we write

SET @uids := null;
UPDATE footable
   SET foo = 'bar'
 WHERE fooid > 5
   AND (SELECT @uids := CONCAT_WS(',', fooid, @uids));
SELECT @uids;

to define the @uids variable that has the ID of the rows we’re updating.

We assign the value for @uids after the AND.

We get all the rows we’re updating with

SELECT @uids := CONCAT_WS(',', fooid, @uids)

@uids is a string with the IDs of the rows we’re updating separated by commas.

Conclusion

To get the ID of the last updated row in MySQL, we can define a variable that has the ID of the entry we’re updating.

Leave a Reply

Your email address will not be published.