2013-11-20 158 views
0

我有以下3个表,Data_Excel包含人的姓名,地址,城市和来源; Person表具有名称和ID;我需要插入person_location地址源,地址,城市和ID ...其中ID来自人表名称存在针对ID应在data_excel表进行匹配来获取所有的细节SQL插入从多个表中选择

I have the following 3 tables, Data_Excel contains the names, address, city and source of person; Person table has the name and ID; I need to insert into person_location the address source, address, city and ID...where ID comes from person table and name that exist against the id should be matched in the data_excel table to get all the details

+3

到目前为止您尝试了什么?你有没有给出预期结果的查询? – Walls

+0

选择A.ID,A.P_name,来源,P_address,P_city,P_country 从data_excel,人A,其中A.name,A.ID在 (选择ID,名称 从人 其中ID> 6566 ) 我试图选择这样的,但得到了一个错误 –

回答

1

看看very similar question这应该提供你需要的信息以适用于你自己的问题。

+0

我将如何检查与id相匹配的名称,以便获得相关的值? –

1

该错误可能是从查询A.name, A.ID in (Select[...]

你可以试试,这部分..

INSERT INTO person_location 
SELECT A.ID,A.P_name,source,P_address,P_city,P_country from data_excel de, person A where A.name = de.c_name; 

如果您需要ID > 6566条件,你可以在末尾添加它。

INSERT INTO person_location 
SELECT A.ID,A.P_name,source,P_address,P_city,P_country from data_excel de, person A where A.name = de.c_name and ID > 6566; 
+0

它给出了一个错误:“列不明确定义” –

+2

取决于你希望从哪个表中取代'SELECT'中的'source'与'A.source'或'de.source'。 – RMK

+0

选择A.ID,A.Name,de.source,de.P_address,de.P_city,de.P_Country from data_excel de,person A 其中A.name = de.P_Name 和ID> 6566 这个作品谢谢... –