We help IT Professionals succeed at work.

how to populate mysql associative tables?

CCVOIP
CCVOIP asked
on
I have a many to many relationship between 2table => an associative table between them
how can I populate this table at the same time I populate the 2 parents tables?
Comment
Watch Question

OK, I'll make up an example for clarity.

Book
* ID
* Title

Author
* ID
* Name

BookAuthor
* BookID
* AuthorID

Insert Into Book (Title) VALUES ('20,000 Leagues Under the Sea');
Select Last_Insert_ID(); /* This retrieves the ID from the previous statement. You can store it in a variable. */
Insert Into Author (Name) VALUES ('Jules Verne');
Select Last_Insert_ID(); /* Please store it in another variable */
Insert into BookAuthor (BookID, AuthorID) Values (FirstVariable, SecondVariable);