Sql 基于所有列的组合更新表
我正在尝试根据所有列的组合更新列 表ASql 基于所有列的组合更新表,sql,oracle,plsql,Sql,Oracle,Plsql,我正在尝试根据所有列的组合更新列 表A NUM_1 NUM_2 NUM_3 Name ----- ----- ----- ---- 1 4 6 Test1 4 4 5 Test2 4 4 3 Test3 表B NUM_1 NUM_2 NUM_3 Name ----- ----- ----- ---- 1 4 6 Final_1 4 4 5 Final_2 4
NUM_1 NUM_2 NUM_3 Name
----- ----- ----- ----
1 4 6 Test1
4 4 5 Test2
4 4 3 Test3
表B
NUM_1 NUM_2 NUM_3 Name
----- ----- ----- ----
1 4 6 Final_1
4 4 5 Final_2
4 4 3 Final_3
若三列NUM1、NUM2、num3匹配,那个么我需要用表B中的值更新表A中的名称
是否有使用任何相关查询或其他内容的简单脚本?Oracle不支持ANSI 92连接进行更新,这将使这一过程变得简单,但我们可以通过合并实现同样的目的
merge into tableA a
using ( select * from tableB ) b
on ( a.num1 = b.num1
and a.num2 = b.num2
and a.num3 = b.num3)
when matched then
update
set a.name = b.name
/
注意:此解决方案假定
(num1、num2、num3)
是表B
上的唯一键。但是任何解决方案都需要这样的唯一性(否则您如何知道应用于tableA
的name
的哪个实例?)。Oracle不支持ANSI 92连接进行更新,这将使这一点变得简单,但我们可以通过合并实现同样的效果
merge into tableA a
using ( select * from tableB ) b
on ( a.num1 = b.num1
and a.num2 = b.num2
and a.num3 = b.num3)
when matched then
update
set a.name = b.name
/
注意:此解决方案假定(num1、num2、num3)
是表B
上的唯一键。但任何解决方案都需要这样的唯一性(否则,您如何知道将名称的哪个实例应用于表a
?)。另一个选项:
SQL> select * From a;
NUM1 NUM2 NUM3 NAME
---------- ---------- ---------- --------------------
1 4 6 test1 --> according to table B data, this
4 4 5 test2 --> and this NAME should be updated
4 4 0 test3
1 2 3 test4
SQL> select * From b;
NUM1 NUM2 NUM3 NAME
---------- ---------- ---------- --------------------
1 4 6 final1
4 4 5 final2
4 4 3 final3
SQL> update a set
2 a.name = (select b.name from b
3 where b.num1 = a.num1
4 and b.num2 = a.num2
5 and b.num3 = a.num3
6 )
7 where exists (select null from b
8 where b.num1 = a.num1
9 and b.num2 = a.num2
10 and b.num3 = a.num3
11 );
2 rows updated.
SQL>
SQL> select * From a;
NUM1 NUM2 NUM3 NAME
---------- ---------- ---------- --------------------
1 4 6 final1
4 4 5 final2
4 4 0 test3
1 2 3 test4
SQL>
另一种选择:
SQL> select * From a;
NUM1 NUM2 NUM3 NAME
---------- ---------- ---------- --------------------
1 4 6 test1 --> according to table B data, this
4 4 5 test2 --> and this NAME should be updated
4 4 0 test3
1 2 3 test4
SQL> select * From b;
NUM1 NUM2 NUM3 NAME
---------- ---------- ---------- --------------------
1 4 6 final1
4 4 5 final2
4 4 3 final3
SQL> update a set
2 a.name = (select b.name from b
3 where b.num1 = a.num1
4 and b.num2 = a.num2
5 and b.num3 = a.num3
6 )
7 where exists (select null from b
8 where b.num1 = a.num1
9 and b.num2 = a.num2
10 and b.num3 = a.num3
11 );
2 rows updated.
SQL>
SQL> select * From a;
NUM1 NUM2 NUM3 NAME
---------- ---------- ---------- --------------------
1 4 6 final1
4 4 5 final2
4 4 0 test3
1 2 3 test4
SQL>
你能分享你的表格结构,并更详细地解释问题吗?你能分享你的表格结构,并更详细地解释问题吗?