I want to get last inserted row id in mysql any one help me Table field is
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
If you want to get the id just after the insertion, use
LAST_INSERT_ID()
:This will return the
AUTO_INCREMENT
value of the last inserted element (in your current connection).If you want to know which is the last inserted value in a determined table, use any of the queries provided in the other answers.
You can use LAST_INSERT_ID(), but you should be aware that (you not only should have the AUTO_INCREMENENT), but it does operates in at connection level. This is, it will return the last_insert_id() for the current connection and not the last_insert_id() that another connection --that might be happening meanwhile-- generated.
Example (lets say the operations arrive the database in the following order):
You should be aware that last-insert_id() operates on a connection level, therefore you want to keep the connection open until you are finished. You should not use things like do a max on the database, because on a web environment you don't have control on how many users will use your app at same time, and in which order the operations will be executed and most important you want to keep your database consistent.