troubleshooting Question

Help with MySQL last_Insert_id() on multiple insert

Avatar of wiredmonkey
wiredmonkey asked on
4 Comments1 Solution517 ViewsLast Modified:


I have a multiquery statement like the example below where the first query inserts a record.

The second query then adds a relationship to another table using the last_insert_id() function.

The third query I wish to also put the last_insert_id() value from the first query but it's obviously taking the id of the second query.

is there any way to save or reuse the last_insert_id() from the first query? (for use in the 3rd query)

INSERT INTO tbl_contacts (name)
VALUES ("Shaun Perry");
INSERT INTO lnk_contacts (contactId)
INSERT INTO lnk_projects (projectId, contactId)

Kind regards


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros