Currently looking to extract data from 2 tables and insert into another.
Tables as follows (with example ideal data):
Table1
T1ID(PK) T1Field1
301 2011-07-01
302 2011-07-01
Table2
T2ID(PK) T2Field2 T2Field3 T2Field4
521 301 555 Apple
522 301 666 Pear
Table3 (new table)
T3Field1 T3Field2 T3Field3 T3Field4
301 2011-07-01 555 Apple
301 2011-07-01 666 Pear
Looking for the fewest and most efficient query’s to use in order to do the following:
-
Insert T1ID into T3Field1 + Insert T1Field1 into T3Field2 -
Update T3Field3 where T3Field1 = T2Field2 (for each) -*
Have simply used mysql insert for the first query. This NEEDS to be done first as it contains timestamp information.
*Attempted INNER JOIN here with no luck since there is a one-to-many and only the first row would get inserted.
I could select all of the results and then while loop / update within for each record but am trying to keep this as minimal as possible.
Just need a second opinion on the logic!
I tested the below code and it matches your ideal data. But, second row of
Table1is lost since there is no corresponding match for it inTable2