1

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.

Joe
  • 4,852
  • 10
  • 63
  • 82
  • What is the error you are getting? also what is the primary key on the Tracking Table? – David Steele Aug 09 '11 at 12:17
  • What error is mysql giving you? It usually has a number/error_id. – ace Aug 09 '11 at 12:17
  • ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT t.tr_id, p.p_id, h.h_id – Joe Aug 09 '11 at 12:24
  • @Joe I have updated the answer and provided a reference as well. Let me know if the updated syntax works. – reggie Aug 09 '11 at 13:12

2 Answers2

2

You can skip the Values infront of select, and you could use the keyword "USING" if you want to connect the tables on a column that have the same name in both tables. i should have used:

INSERT INTO tracking (tr_id, p_id, h_id)
SELECT tr_id, p_id, h_id
FROM transactions
LEFT JOIN people USING (tr_id)
LEFT JOIN holidays USING (tr_id)
Puggan Se
  • 5,738
  • 2
  • 22
  • 48
1

There is a syntax error. When you use Join, it is followed by the ON keyword which specifies which columns need to match. The following query will work:

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 
on t.tr_id = p.tr_id
JOIN holidays h
on t.tr_id = h.tr_id)

You can read more about JOINS over here

UPDATE

This should work:

INSERT INTO tracking (tr_id, p_id, h_id) 
SELECT t.tr_id, p.p_id, h.h_id
FROM transactions t 
JOIN people p 
on t.tr_id = p.tr_id
JOIN holidays h
on t.tr_id = h.tr_id

Reference: MySQL Insert & Joins

The reference that I have provided does not use the VALUES keyword in the query. Strange syntax, but let me know if that works.

Community
  • 1
  • 1
reggie
  • 13,313
  • 13
  • 41
  • 57
  • Thanks for your answer. I'm convinced this is what I need, but I keep getting an ERROR 1064 (42000) message whenever I use it. Strangely if I just use the outer query with some explicit values it works, and if I use the inner query on it's own, I get the values that I want to insert. – Joe Aug 09 '11 at 12:52
  • @Joe What are the other columns in your `tracking` table? Does any of the other columns are defined such as they cannot contain null values? Secondly, is `tr_id, p_id, h_id` are defined as `INT` in all the tables? – reggie Aug 09 '11 at 13:01
  • There are no other columns in the tracking table (apart from the auto incremented track_id). All the other columns can contain null values. All the values are INT(11). Not sure what's going on here. – Joe Aug 09 '11 at 13:25
  • 1
    @Joe I have updated my answer with another query. Have you tried that? the syntax is slightly customized. Provided a reference as well. Let me know – reggie Aug 09 '11 at 13:42
  • 1
    -Thanks so much. That's working now. That really is strange. I really appreciate your help, thanks for spending time on this :) – Joe Aug 09 '11 at 13:46
  • No problem Joe. I am happy I could help you out on this. – reggie Aug 09 '11 at 13:54
  • @Joe let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2272/discussion-between-reggie-and-joe) – reggie Aug 09 '11 at 13:54