【问题标题】:SQL Insert into with select from multiple tablesSQL Insert into with select from multiple tables
【发布时间】:2013-11-20 14:38:30
【问题描述】:

我有以下3个表,Data_Excel包含人的姓名、地址、城市和来源; Person表有名字和ID;我需要将地址源、地址、城市和 ID 插入到 person_location 中...其中 ID 来自 person 表,并且与 id 相对应的名称应该在 data_excel 表中匹配以获取所有详细信息

【问题讨论】:

  • 到目前为止您尝试过什么?您是否有未提供预期结果的查询?
  • Select A.ID,A.P_name,source,P_address,P_city,P_country from data_excel, person A where A.name, A.ID in (Select id,name from person where ID > 6566 )我尝试像这样选择,但出现错误

标签: sql oracle-sqldeveloper sql-insert


【解决方案1】:

看看这个very similar question,它应该提供你需要的信息来解决你自己的问题。

【讨论】:

  • 如何检查名称与 id 的匹配情况,以便获得相关值?
【解决方案2】:

错误可能来自查询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;

【讨论】:

  • SELECT 中的source 替换为A.sourcede.source,具体取决于您要从哪个表获得。
  • Select A.ID, A.Name , de.source, de.P_address, de.P_city, de.P_Country from data_excel de, person A where A.name = de.P_Name and ID > 6566这行得通,谢谢...
  • 不客气。在这种设置中要小心,但如果有多个同名的 ID,它可能会变得混乱。
  • 在这种情况下,如何对 id+name 组合进行唯一检查?
  • 我也尝试插入,但显然 start_date 是主键,你能告诉我如何增加日期值吗?插入 person_location(person,address,city,country,startdate,enddate,source) Select A.ID,de.P_address, de.P_city, de.P_Country,Select dateadd(mm,1,'01/01/1000', 'DD/MM/YYYY'),to_date('31/12/3999','DD/MM/YYYY'), de.source from data_excel de, person A where A.name = de.P_Name and ID > 6566
猜你喜欢
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 2022-08-02
相关资源
最近更新 更多