I have 3 tables:
Everyone of them with its own AUTO_INCREMENT id (id_customer, id_email, id_phone)
and I have 2 more tables to relate them:
These tables contain the others's tables ids as foreign keys.
How can I relate them in a single SQL statement using LAST_INSERT_ID()
? Or what do you recommend?
You can store the results of LAST_INSERT_ID() using variables.
INSERT INTO email(...) VALUES (...);
SET @email_id = LAST_INSERT_ID();
INSERT INTO phone(...) VALUES (...);
SET @phone_id = LAST_INSERT_ID();
INSERT INTO customer_phone(id_email, id_phone) VALUES(@email_id, @phone_id);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments