1

How can I get the auto_increment value that MySQL generated for the current insert row, so I can use it in the insert command? For example:

INSERT INTO aTable (aField) VALUES ( AUTOINCVALUEHERE )

This does not seem like a complicated operation, but I have been unable to find any solution here, in the MySQL documentation, or otherwise. I am aware of the potential of executing an UPDATE, but I would much prefer if I did not need to run multiple queries to accomplish this.

I have already tried LAST_INSERT_ID() - it does not work, and the docs say it only is intended to give the value from the last query executed, not the current one.

Bakaiya
  • 43
  • 5
  • possible duplicate of [MySQL trigger to update a field to the value of id](http://stackoverflow.com/questions/1211792/mysql-trigger-to-update-a-field-to-the-value-of-id) – Bill Karwin Feb 11 '13 at 03:25
  • Do you use InnoDB? Do you set autocommit = 1? If Yes/No you can wrap your insert query with `start transaction`, `commit` and inside of it use `last_insert_id()` – ravnur Feb 11 '13 at 08:45

1 Answers1

0

you need to have another unique field in table for example -email-id ,so you can get respective autoincremented_id from that row. but still confuse why you want to use that in insert query ?

 "select autoincremented_id from Table where somthing_unique = value" ;
ketul shah
  • 413
  • 2
  • 7
  • 17