0

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.

isumit
  • 2,313
  • 4
  • 23
  • 28
  • 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 – isumit Oct 14 '15 at 15:55
  • they can have in future but for now i need table 3 to have one row per row in table 1. Its kind of a back-fill where i need to fill data in these tables one time. – isumit Oct 14 '15 at 16:03
  • table 1 is like user table and table 3 is address table. So i need query to set one address per user. – isumit Oct 14 '15 at 16:06
  • OK, I tried. Forget it. – Strawberry Oct 14 '15 at 16:09
  • np, thanks for your time. – isumit Oct 14 '15 at 16:11

0 Answers0