Clicky

mySql - Get Last Inserted ID

January 1, 2011

Given the scenario where you have the tables:

  • Clients and Clients_Addresses

In order to retrieve the id of the last client inserted in Clients you may do:

  • select LAST_INSERT_ID()

This will then retrieve the last inserted id, but looking at the query you will notices that you don’t have the table. So this solution will only work if you want to retrieve the id of the last inserted row in all your database, so be aware of concurrency problems.

If this solution does not fit, you may construct you table in a way that the id is generated with AUTO_INCREMENT flag, than simply issue a

  • select max(cli_id) from Clients

and use this value to add in the Clients_Addresses table