-1

I have two tables table1 and table2 with some data and the id of table 1 and table 2 both are the foreign keys in table3.

How can I write a SQL statement to insert the data from table1 and table2 into table3?

I have data in table1 and table2 but no data in table3 - I have to populate that using the other two.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
smsh
  • 185
  • 2
  • 2
  • 13
  • I have tried to set a relation like tried joining two tables with the with the one but there is no data in the table3 so it hasn't worked – smsh Feb 26 '15 at 15:27
  • what do you mean when you say you have to populate table3? you want to blindy insert all combination of table1 and 2? – Fredou Feb 26 '15 at 15:32

1 Answers1

0

Use an Update with joins Something like this...

UPDATE t3
SET x=A, y=B, z=C
FROM TABLE3 t3
inner join table1 t1
on t3.tbl1id=t1.id 
inner join table2 t2
on t3.tbl2id=t2.id 
SoulTrain
  • 1,904
  • 1
  • 12
  • 11
  • I want to update only the Ids of table 1 and table 2 in table3 nothing else than that only ids – smsh Feb 26 '15 at 15:15
  • the x,y and z in the above query could be any column in table3, including ids of table1 and table2. But realize this, once you do that you might break the mapping chain between Table3 and table1/ table2, since you are using those columns to join with those tables. – SoulTrain Feb 26 '15 at 15:18
  • BUt i have no data in the table3 how it will compare and update in table 3 – smsh Feb 26 '15 at 15:24
  • I see. Can you correlate table1 and table2? if yes, then you can write an insert query based on that for table3. – SoulTrain Feb 26 '15 at 15:31