Currently I am working with a project . I required to find last updated id.How can I get last updated row ID in mysql ? Any one help please ?
2 Answers
When a new AUTO_INCREMENT
value has been generated, you can also obtain it by executing a SELECT LAST_INSERT_ID()
statement with mysql_query()
and retrieving the value from the result set returned by the statement.
For LAST_INSERT_ID()
, the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT
column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID()
and AUTO_INCREMENT
columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.

- 78,589
- 36
- 144
- 183
First, edit your table so that it will automatically keep track of whenever a row is modified. Remove the last line if you only want to know when a row was initially inserted.
ALTER TABLE mytable
ADD lastmodified TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
Then, to find out the last updated row, you can use this code.
SELECT id FROM mytable ORDER BY lastmodified DESC LIMIT 1;

- 61,834
- 16
- 105
- 160