tags:

views:

64

answers:

1

I'm subsequently inserting a single record in two tables. With the first insert i put the record in the main table, where the primary key value gets generated. Now i need to put some fields of this record in another table including the key value of the main table. How do i do this?

+3  A: 

"SELECT LAST_INSERT_ID();" will return the last autoincrement ID generated.

Jorge Villuendas
And a thread-safe way to do this?
akosch
LAST_INSERT_ID() is thread-safe. It's stored on a per-connection basis, so insert statements from others connections will not affect its value.
Jorge Villuendas
If akosch is sharing connections between threads (yuck) the insert may need to be in a stored function that returns the last_insert_id().
ysth
Well, I would better redesign the application to not share the same opened connection between several threads, unless he has a good reason for doing so.
Jorge Villuendas