I want to execute a query of the form:
INSERT INTO table(field1, field2) SELECT field1, field2 FROM table WHERE id IN ( 4, 8, 15, 16, 23, 42 )
Where id is the auto_increment’ing primary key of table.
After executing this statement, I want to know what all the new id’s that just got generated are. How can I recover this information? I’m interested in (maybe) doing this in a stored procedure and returning the list of newly generated id’s to my application code (lets say PHP), and I would like the list to be in correlated order to what appears in the IN clause of the SELECT subquery. I’m thinking this would save me a lot of sequential INSERTs in my application code. Is that achievable?
If you have control over the table (or can create another table) how about storing a “transaction_id?”
If you added it to your table, you could do something like this:
You’d then pull out the new Ids:
As a note, you’d either need to run that query immediately as part of your stored procedure, or you’d need to store the transactionId you used somewhere so that you can pull out the correct rows without worrying about multiple concurrent inserts.