I have the following table schema:
Table_1 :
Id(PK, autoIncrement)
Name
Table_2 :
Table1_Id
Table3_Id
Table_3 :
Id (PK, autoIncrement)
Table_2 has combination of Ids of Table_1 and Table_3 as its primary key. I need to insert values in Table_2 and Table_3 for all rows of Table_1. I am trying something like :
Insert into Table_2(Table1_Id) Select Table_1.Id from Table_1
But My problem is Table_1 and Table_3 have no direct relation and Table_2 needs Id of Table_3. Is it possible to Insert and get the new Id from table_3 within the above query(insert into Table_2) ? Please suggest a way to achieve this using mysql query only.
Below is the idea of something that if possible could have helped. Please ignore the syntax here:
Insert into Table_2(Table1_Id, Table3_Id) Select Table_1.Id, (Insert into Table_3 values (some_values..)) from Table_1
(If the inner insert query would have returned newly added Id )
To explain further here is a rough example:
Currently table 1 have a and b, now i want to add new x and y in table 3. And then table 2 will have 2 rows with a,x and b,y. A & x are linked but through table 2. And for now only table 1 has rows, other two tables are empty
Please let me know if i am not clear here explaining the problem.