I have a table named people_temp_1 that contains:
Name Age PersonID
John 25 1
Jane 32 2
Chris 47 3
And another named people_temp_2 that contains additional information that is linked by the PersonID in table1:
ID Profession Location
1 Web Developer Texas
2 Graphic Designer North Carolina
3 Sales California
And I want to create a new table called people that "merges" the data from both tables. How I am doing this now is:
INSERT INTO people(name, age, profession, location)
SELECT people_temp_1.name AS name,
people_temp_2.age AS age,
(SELECT people_temp_2.profession FROM people_temp_2 WHERE people_temp_2.id = people_temp_1.personId) AS profession,
(SELECT people_temp_2.location FROM people_temp_2 WHERE people_temp_2.id = people_temp_1.personId) AS location
FROM people_temp_1
As you can see I am using multiple select queries in the insert into query in order to get data that is relevant via the personId. It seems a little dirty to be doing multiple select queries when I should somehow be able to query the people_temp_2 table once and use all of its columns, but I can't figure out how to do that.
Is there a better way to structure that insert into statement?