How to get ID of the last updated row in MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
There is no direct MySQL equivalent of LAST_INSERT_ID() for UPDATE statements. That is the key fact behind this question. An update does not automatically produce one special "last updated row ID," especially when the statement can affect multiple rows. If you need that ID, the correct approach depends on how the update is written and what "last updated" is supposed to mean in your application.
If you update by primary key, you already know the ID
The simplest case is an update that targets one row by its primary key. In that case, the ID is not something MySQL needs to discover later, because you already used it in the WHERE clause.
Here the updated row ID is 42. There is nothing else to ask the database afterward.
This sounds obvious, but it is the right answer surprisingly often.
Multi-row updates do not have one natural "last row"
A statement like this may update many rows:
What would the "last updated row" mean here?
- the row with the highest ID?
- the last row visited internally by the engine?
- the row most recently modified by timestamp?
SQL does not define one special answer. For multi-row updates, you need your own rule and usually your own query or schema support.
Add an updated_at column if you need "most recently changed"
If the business question is really "which row was updated most recently," add a timestamp column and write your update so it records that fact.
Then you can query by that timestamp:
This gives meaning to "last updated." Without a stored timestamp or log, the concept does not exist reliably.
Use transactions when the update and follow-up must stay connected
If you update one row and then immediately need its data back, wrap the logic in a transaction and query by the same key.
This is much more reliable than trying to infer some session-global "last updated ID" that MySQL does not actually track.
Use an audit table or trigger for history
If you need to know which rows were changed over time, not just in one immediate code path, record that history explicitly.
Then your application or trigger can insert into that log whenever a row changes. Once you do that, "last updated row" becomes a query against the log rather than a guess.
Why LAST_INSERT_ID() does not help
LAST_INSERT_ID() is specifically about auto-increment inserts. It does not tell you which row an UPDATE touched. If you see code trying to call it after an update, that is a sign the wrong mental model is being applied.
This is one of the most common misunderstandings around MySQL metadata functions.
Common Pitfalls
The biggest mistake is assuming MySQL remembers one global "last updated row ID" for an UPDATE. It does not.
Another issue is asking for the last updated row after a multi-row update without defining what "last" should mean. SQL engines do not guarantee an application-friendly row order for that idea.
People also reach for LAST_INSERT_ID() because it sounds close to the problem. It is only for insert-generated auto-increment values.
Finally, if you truly need update history, store it explicitly with timestamps, audit tables, or triggers instead of trying to reconstruct it after the fact.
Summary
- MySQL has no built-in
LAST_UPDATE_ID()function. - If you update by primary key, you already know which row was changed.
- Multi-row updates do not have one meaningful built-in "last updated row."
- Add
updated_ator audit logging if you need to query the most recently changed row later. - Do not use
LAST_INSERT_ID()to solve an update-tracking problem.
Related reading
- How to get input file name as column in AWS Athena external tables
- How to get kafka message's headers in Kafka Connect Sink connector with MongoDB
- How to get last insert id after insert query inside of a transaction in CodeIgniter
- How to get next/previous record in MySQL?
- How to get the count of each distinct value in a column?
- How to get the identity of an inserted row?
- How to get the index of an element in an IEnumerable?
- How to get the insert ID in JDBC?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.