I am trying to write a query that will insert rows into two tables, tableA and tableB. tableA is a list of emails that can be sent out to uses while tableB contains the actual text of said emails in the various languages that we support. The auto-increment id from tableA gets stored in tableB to connect the email names with the text.
What is the best way to grab the id from the new row inserted into tableA to then use in the queries inserting into tableB? I tried this but mySQL didn’t like it (error 1064):
INSERT INTO tableA (eID, name, otherStuff) VALUES (NULL, 'emailName', 'otherValues');
INSERT INTO tableB (id, emailId, body, name, langId) VALUES (NULL, select max(tableA.eID) from tableA, 'email text', 'Default', '1');
INSERT INTO tableB (id, emailId, body, name, langId) VALUES (NULL, select max(tableA.eID) from tableA, 'other language text', 'Default', '2');
Any thoughts on how to make this work?
thanks
The mysql function LAST_INSERT_ID() will return you the id of the last inserted row.
So you could try