cobogarciaj
cobogarciaj

Reputation: 3

SQL insert into multiples tables

I want to do an insert on two tables, one value of the second table field is the value of one field on the first table. The problem is: this value is AutoIncrement and is generated when the insert in the table. So how can i do it?

Thanks

Upvotes: 0

Views: 56

Answers (1)

Rufinus
Rufinus

Reputation: 30721

see http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

Quote:

if you want to use the ID that was generated for one table and insert it into a second table, you can use SQL statements like this:

INSERT INTO foo (auto,text)
    VALUES(NULL,'text');         # generate ID by inserting NULL
INSERT INTO foo2 (id,text)
    VALUES(LAST_INSERT_ID(),'text');  # use ID in second table

Upvotes: 1

Related Questions