从多个表中插入

时间:2011-02-08 07:46:01

标签: sql

我正在尝试从3个表中插入数据,这些表将CSV文件中的数据保存到有意义的仓库表中,但不知道我的语法是否正确。

insert into warehouseTBL
select a.recordNum, b.fieldA, b.fieldB, c.fieldC, c.fieldD,
  from ctrTable a, tableB b, tableC c
 where a.recordNum = b.recordNum
   and a.recordNum = c.recordNum
   and a.someField = b.someField
   and a.someField = c.someField

因此,我应该在From子句中使用嵌套选择,使其看起来像这样:

  from ctrTable a, (
        select *
          from tableB, tableC,
         where tableB.recNum = tableC.recNum
       ) as d,
 where a.recNum      = d.recNum

语法是否有意义?

1 个答案:

答案 0 :(得分:4)

使用此查询插入行

insert into warehouseTBL
select recordNum, fieldA, fieldB, fieldC,fieldD from 
(select a.recordNum, b.fieldA, b.fieldB, c.fieldC, c.fieldD,
  from ctrTable a, tableB b, tableC c
 where a.recordNum = b.recordNum
   and a.recordNum = c.recordNum
   and a.someField = b.someField
   and a.someField = c.someField)p