【问题标题】:update a column with another table column in sql with the same column being the id用sql中的另一个表列更新一个列,同一列是id
【发布时间】:2014-10-28 01:04:16
【问题描述】:

我正在尝试更新 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 命令未正确结束”

【问题讨论】:

标签: oracle


【解决方案1】:

我认为您的问题是您在 tableB 中有多个匹配的行(“其中 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 ...

【讨论】:

    猜你喜欢
    • 2011-04-18
    • 2022-01-23
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多