Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql ORACLE:使用具有关系的两个表更新查询_Sql_Oracle_Sql Update - Fatal编程技术网

Sql ORACLE:使用具有关系的两个表更新查询

Sql ORACLE:使用具有关系的两个表更新查询,sql,oracle,sql-update,Sql,Oracle,Sql Update,我需要根据两个表中找到的匹配记录更新列数据。 我想更新表2中NAME列的记录 以下是表格 Table1 --------------- Id | Name | color 1 | abc | red 2 | def | green 3 | ghi | blue Table2 --------------- Id | Name | color |fiedId 1 | abc | red | 1 2 | def | green | 1 3 | ghi

我需要根据两个表中找到的匹配记录更新列数据。 我想更新表2中NAME列的记录

以下是表格

 Table1
---------------
 Id | Name | color
 1  | abc  | red
 2  | def  | green
 3  | ghi  | blue


 Table2
---------------
 Id | Name | color |fiedId
 1  | abc  | red   | 1
 2  | def  | green | 1
 3  | ghi  | blue  | 2
这里table1 ID列是table2中的外键,作为fieldId

因此,我想更新所有在此条件下的记录

table1.id = table2.fieldId

听起来您只需要这样的更新:

update table2 t2
set    t2.name =
       ( select t1.name
         from   table1 t1
         where  t1.id = t2.fieldid )
关于后续问题:

如果要为所有匹配行设置Name=“xxx”,该怎么办

或者可以写为:

update table2 t2
set    t2.name = 'xxx'
where  exists
       ( select null from table1 t1
         where  t1.id = t2.fieldid )

听起来您只需要这样的更新:

update table2 t2
set    t2.name =
       ( select t1.name
         from   table1 t1
         where  t1.id = t2.fieldid )
关于后续问题:

如果要为所有匹配行设置Name=“xxx”,该怎么办

或者可以写为:

update table2 t2
set    t2.name = 'xxx'
where  exists
       ( select null from table1 t1
         where  t1.id = t2.fieldid )

还有一个选项,使用
合并

merge into table2 t2
using (select id, name from table1) x
on (t2.fieldid = x.id)
when matched then update set
  t2.name = x.name;
或者,将名称设置为“xxx”:

merge into table2 t2
using (select id from table1) x
on (t2.fiedid = x.id)
when matched then update set
  t2.name = 'xxx';

还有一个选项,使用
合并

merge into table2 t2
using (select id, name from table1) x
on (t2.fieldid = x.id)
when matched then update set
  t2.name = x.name;
或者,将名称设置为“xxx”:

merge into table2 t2
using (select id from table1) x
on (t2.fiedid = x.id)
when matched then update set
  t2.name = 'xxx';

更新哪个表中的记录?将什么更新为什么?我想更新表2中的名称到目前为止您的尝试是什么?更新哪个表中的记录?并将什么更新为什么?我想更新表2中的名称到目前为止您的尝试是什么?如果我想为所有匹配行设置name=“xxx”怎么办?如果我想为所有匹配行设置name=“xxx”怎么办?