我有一个名为 people_temp_1 的表,其中包含:
Name Age PersonID
John 25 1
Jane 32 2
Chris 47 3
另一个名为 people_temp_2 的文件包含由 table1 中的 PersonID 链接的附加信息:
ID Profession Location
1 Web Developer Texas
2 Graphic Designer North Carolina
3 Sales California
我想创建一个名为 people 的新表,它“合并”两个表中的数据。我现在的做法是:
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
如您所见,我在插入查询中使用了多个选择查询,以便通过 personId 获取相关数据。当我应该能够以某种方式查询 people_temp_2 表并使用它的所有列时,执行多个选择查询似乎有点脏,但我不知道该怎么做。
有没有更好的方法来构造插入语句?