如何在Oracle中多次更新与另一个表联接的表?

时间:2018-12-11 07:41:03

标签: sql oracle sql-update oracle10g

我的表中有三个字段,我想通过三次连接到另一个表来更新它们。我知道我必须使用merge into,但是找不到使用merge intojoin且只有一个表多次的类似查询。

select语句如下:

select * from TABLE t
inner join DESTINATION_TABLE d1
on t.CODE1 = d1.CODE
inner join DESTINATION_TABLE d2
on t.CODE2 = d2.CODE
inner join DESTINATION_TABLE d3
on t.CODE3 = d3.CODE

现在如何使用FIELD1FIELD2中的字段来更新FIELD3中的三个字段(TABLEd1d2) ,和d3使用merge into

编辑:

原始查询是:

select * from TOTAL
inner join GROUP_LEVEL_DETAIL gl1
on gl1.NAME = substr(GL, 1, instr(GL, ' -', 1))
inner join GROUP_LEVEL_DETAIL gl2
on GL2.NAME = replace(substr(GL, instr(GL, ' -', 1, 1), instr(GL, ' -', 1, 2) - instr(GL, ' -', 1, 1)), ' - ', '')
inner join GROUP_LEVEL_DETAIL gl3
on gl3.NAME = replace(substr(GL, instr(GL, '-', 1, 2), 500), '- ', '')

TOTAL的示例数据为:

  ID                GL                GL1_CODE   GL2_CODE   GL3_CODE
-----  ----------------------------- ---------- ---------- -----------
  1     Sample1 - Sample2 - Sample3    null        null       null
  2     John - Jack - Harry            null        null       null

GROUP_LEVEL_DETAIL的示例数据为:

  CODE         NAME        LEVEL_NO
---------  -----------   ------------
  SMP1       Sample1           1
  SMP2       Sample2           2
  SMP3       Sample3           3
  JCK1       Jack              1
  JHN2       John              2
  HRY3       Harry             3

我希望我的TOTAL表在更新后变得像这样:

  ID                GL                GL1_CODE   GL2_CODE   GL3_CODE
-----  ----------------------------- ---------- ---------- -----------
  1     Sample1 - Sample2 - Sample3    SMP1        SMP2       SMP3
  2     John - Jack - Harry            JCK1        JHN2       HRY3

2 个答案:

答案 0 :(得分:1)

从您修改后的问题来看,我认为解决方案是在子查询中组装GROUP_LEVEL_DETAIL名称,以创建一个可以加入TOTAL表的键。

merge into TOTAL t
using ( select d1.code as d1_code
               , d2.code as d2_code
               , d3.code as d3_code
               , d1.name || ' - ' ||
                 d2.name || ' - ' ||
                 d3.name as joined_code
         from  GROUP_LEVEL_DETAIL d1
         cross join GROUP_LEVEL_DETAIL d2
         cross join GROUP_LEVEL_DETAIL d3
         where d1.level_no = 1
         and   d2.level_no = 2
         and   d3.level_no = 3  
       ) d
on ( t.gl = d.joined_code )
when matched then
    update 
    set t.gl_code1 = d.d1_code
        ,  t.gl_code2 = d.d2_code
        ,  t.gl_code3 = d.d3_code

USING子查询从GROUP_LEVEL_DETAIL生成记录的所有可能排列的结果集。在您进行修订之后,我加入了WHERE子句以强制执行隐含规则GL = 'level 1 - level 2 - level 3'。您可能不希望这样做(也许并非TOTAL中的每个记录都具有有效的GL),或者扩展WHERE子句以应用关于有效组合的任何其他规则。

答案 1 :(得分:0)

当联接基于主键或唯一索引时,您应该能够执行以下操作:

--create tables and unix index
create table table1 (code number, field1 number, field2 number, field3 number);
create unique index table1_code_inx on table1 (code);

create table table2 (code number, field1 number, field2 number, field3 number);
create unique index table2_code_inx on table2 (code);

-- update data from table2 to table1
update( select a.FIELD1 A_FIELD1, a.FIELD2 A_FILED2, a.FIELD3 A_FIELD3,
               b.FIELD1 B_FIELD1, b.FIELD2 B_FILED2, b.FIELD3 B_FIELD3 
          from TABLE1 a, TABLE2 b
          where a.CODE = b.CODE )
set A_FIELD1 = B_FIELD1,
    A_FILED2 = B_FILED2,
    A_FIELD3 = B_FIELD3;