How do I use the information from existing MySQL tables to populate a new table. I have three tables each of which have a key that I want to include in the new table like so:
TRANSACTIONS TABLE
tr_id INT NOT NULL AUTO_INCREMENT
other columns
.
.
PEOPLE TABLE
p_id INT NOT NULL AUTO_INCREMENT
tr_id INT
other columns
.
.
HOLIDAYS TABLE
h_id INT NOT NULL AUTO_INCREMENT
tr_id INT
other columns
.
.
TRACKING TABLE
track_id INT NOT NULL AUTO_INCREMENT
tr_id
p_id
h_id
other columns
.
.
I'm not even sure that I need the p_id and h_id columns in the new table, because I just need to be able to get all the rows from the tracking table for a single tr_id, but they may come in useful, so I might leave them in there. (I do need them to create the rows in the new table because one transaction can relate to multiple people going on multiple holiday destinations and each row in the tracking table ust relate to a single destination for a single person, so for 2 people each going to the same 2 holiday destinations, there will be 4 rows in the tracking table)
I have tried:
INSERT INTO tracking (tr_id, p_id, h_id) VALUES
(SELECT t.tr_id, p.p_id, h.h_id
FROM transactions t
JOIN people p
JOIN holidays h
WHERE t.tr_id = p.tr_id
AND t.tr_id = h.tr_id);
but this is giving me an error.
Grateful for any advice on this. Many thanks.