Select last row in MySQL

esqew picture esqew · Nov 2, 2010 · Viewed 541.3k times · Source

How can I SELECT the last row in a MySQL table?

I'm INSERTing data and I need to retrieve a column value from the previous row.

There's an auto_increment in the table.

Answer

Pekka picture Pekka · Nov 2, 2010

Yes, there's an auto_increment in there

If you want the last of all the rows in the table, then this is finally the time where MAX(id) is the right answer! Kind of:

SELECT fields FROM table ORDER BY id DESC LIMIT 1;