Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Select the last inserted row easiest way

Tags:

select

mysql

I simply need to select the last entered row specified by condition, e.g:

SELECT ID from bugs WHERE user=Me 

I need to return only the very last ID entered by user 'Me'. Is there a simple way to do this? Thank you.

like image 566
Thomas Avatar asked May 05 '10 04:05

Thomas


People also ask

How do I get the latest inserted row in MySQL?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL.

How do I get last inserted data?

Get ID of The Last Inserted RecordIf we perform an INSERT or UPDATE on a table with an AUTO_INCREMENT field, we can get the ID of the last inserted/updated record immediately.

How do I get the last inserted row ID in SQL?

MySQL LAST_INSERT_ID() Function The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.

How do I get the unique ID for the last inserted row?

9 Obtaining the Unique ID for the Last Inserted Row. If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.


1 Answers

It would be best to have a TIMESTAMP column that defaults to CURRENT_TIMESTAMP .. it is the only true predictive behavior you can find here.

The second-best thing you can do is ORDER BY ID DESC LIMIT 1 and hope the newest ID is the largest value.

like image 112
Matt Avatar answered Sep 28 '22 15:09

Matt