使用sql中的另一个表列更新列,其中相同的列是id

时间:2014-10-27 16:35:56

标签: oracle

我正在尝试更新tableA中的columnA。 ColumnA的新值是从tableB ColumnB中提取的,使用列a作为ID。我使用以下查询,但我无法更新表。

update tableA a set columnA = (select b.columnB from tableb b where b.columnC = a.columnA) 
where exists (select * from tableb b where b.columnC = a.columnA) and a.columnD = 'ABC'

对于上面的查询,我得到异常'单行子查询返回多行'

update tableA a set a.columnA = b.columnB from tableb b on a.columnA = b.columnC where a.columnD = 'ABC'

对于上述查询,我​​收到异常'SQL命令未正确结束'

update a set a.columnA = b.columnB from tablea a inner join tableb b on a.columnA=b.columnC where a.columnD = 'ABC'

对于上述查询,我​​收到异常'SQL命令未正确结束'

1 个答案:

答案 0 :(得分:0)

我认为你的问题是你在tableB中有多个匹配的行(“where b.columnC = a.columnA”)。所以当你告诉Oracle时:

set columnA = (
select b.columnB 
from tableb b 
where b.columnC = a.columnA)
where exists ...

它在tableB中为给定的键值找到多行。你必须决定如何让Oracle选择一个。例如,如果你真的不关心多个值中的哪一个,你可以做一些像(未经测试的):

set columnA = (
select distinct(max(b.columnB)) 
from tableb b 
where b.columnC = a.columnA)
where exists ...
相关问题