如何在 Oracle 中多次更新与另一个 table 连接的 table?
How to update a table joined with another table multiple times in Oracle?
我的 table 中有三个字段,我想通过三次加入另一个 table 来更新它们。我知道我必须使用 merge into
,但我找不到任何类似的查询多次使用 merge into
和 join
且只有一个 table。
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
现在如何使用 d1
、[=26] 中的字段更新 TABLE
中的三个字段(FIELD1
、FIELD2
、FIELD3
) =],并且 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
table 在更新后变成这样:
ID GL GL1_CODE GL2_CODE GL3_CODE
----- ----------------------------- ---------- ---------- -----------
1 Sample1 - Sample2 - Sample3 SMP1 SMP2 SMP3
2 John - Jack - Harry JCK1 JHN2 HRY3
根据您修改后的问题,我认为解决方案是 assemble 子查询中的 GROUP_LEVEL_DETAIL 名称来创建一个键,您可以将其加入 TOTAL table。
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 子句以应用任何其他有关有效组合的规则。
当连接基于主键或唯一索引时,您应该能够执行以下操作:
--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;
我的 table 中有三个字段,我想通过三次加入另一个 table 来更新它们。我知道我必须使用 merge into
,但我找不到任何类似的查询多次使用 merge into
和 join
且只有一个 table。
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
现在如何使用 d1
、[=26] 中的字段更新 TABLE
中的三个字段(FIELD1
、FIELD2
、FIELD3
) =],并且 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
table 在更新后变成这样:
ID GL GL1_CODE GL2_CODE GL3_CODE
----- ----------------------------- ---------- ---------- -----------
1 Sample1 - Sample2 - Sample3 SMP1 SMP2 SMP3
2 John - Jack - Harry JCK1 JHN2 HRY3
根据您修改后的问题,我认为解决方案是 assemble 子查询中的 GROUP_LEVEL_DETAIL 名称来创建一个键,您可以将其加入 TOTAL table。
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 子句以应用任何其他有关有效组合的规则。
当连接基于主键或唯一索引时,您应该能够执行以下操作:
--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;