0

I have 2 tables named Courses(C_ID, C_NAME, CREATED_AT) & Topics(T_ID, C_ID, T_NAME, CREATED_AT). The C_ID in the Topics table references C_ID in Courses Table.

Now I have to run an insert query to Topics table just after inserting a particular course. But I am not able to figure out how to craft the insert query for Topics table. I have the topics in an array currently.

What I have thought of is:

insert into Topics(C_ID, T_NAME, CREATED_AT) VALUES
((some select statement to get the newly inserted course's C_ID), topic0, NOW()),
((some select statement to get the newly inserted course's C_ID), topic1, NOW()), // Selecting same C_ID as above
((some select statement to get the newly inserted course's C_ID), topic2, NOW()) // Selecting same C_ID as above
...

But I don't want to repeat the same select statement multiple times for each row as it is yielding the same C_ID every time.

Is there any way to do this properly? I am an amateur in SQL. Any help would be appreciated. Thanks in advance!

  • `LAST_INSERT_ID()` contains tha auto-increment ID from the last `INSERT` query. Is that what you're looking for? – Barmar Aug 11 '21 at 20:43

0 Answers0