如何使用来自另一个 table 的数据更新 table

How to update table with data from another table

我有两个table

第一个tableINTRA.TABLE_A

id  | first_value | second_value
----------------------------------
1   |    1234     |    181818
2   |    1235     |    1919191
3   |    1236     |    2384

第二个tableEXTRA.TABLE_B

id  | first_value | second_value
----------------------------------
1   |    1235     |    1919191
2   |    1236     |    0
3   |    1234     |    0
4   |    1234     |    181818
5   |    1234     |    0
6   |    1236     |    0

我正在尝试通过更新将数据从 INTRA.TABLE_A 获取到 EXTRA.TABLE_B

UPDATE B
SET
    B.SECOND_VALUE = A.SECOND_VALUE
FROM
    EXTRA.TABLE_B B,
    INTRA.TABLE_A A
WHERE
    A.FIRST_VALUE = B.FIRST_VALUE
    AND B.SECOND_VALUE = 0;

但它在 FROM 字处“抛出”语法错误:

syntax_error

更新后,结果应如下所示:

id  | first_value | second_value
----------------------------------
1   |    1235     |    1919191
2   |    1236     |    2384
3   |    1234     |    181818
4   |    1234     |    181818
5   |    1234     |    181818
6   |    1236     |    2384

我正在使用 sqlDeveloper 和 Oracle 数据库。我该如何解决?

MERGE会有什么好处吗?

MERGE INTO table_b b
     USING table_a a
        ON (a.FIRST_VALUE = b.FIRST_VALUE)
WHEN MATCHED
THEN
   UPDATE SET b.second_value = a.second_value;

Oracle 不支持 update 查询中的联接 - 与其他数据库不同,例如 SQL 服务器(您正在使用的查询可能 运行 就这样) .

我会推荐一个相关的子查询:

update table_b b
set second_value = (select a.second_value from tablea a where a.first_value = b.first_value)
where b.second_value = 0

您可能想要添加条件以确保仅更新“匹配”行:

update table_b b
set second_value = (select a.second_value from tablea a where a.first_value = b.first_value)
where 
    b.second_value = 0
    and exists (select a.second_value from tablea a where a.first_value = b.first_value)