Oracle SQL - 使用 MINUS 运算符更新列数据

Oracle SQL - Update column data using MINUS operator

我有这个 sql 查询

(select x.a, x.b, x.c, x.d
from tableX x
where x.a IS NOT NULL
minus 
select y.a, y.b, y.c, y.d
from tableY y);

上面的查询returns我的都是tableX的数据,就是和tableY不一样。返回多个元组

证据:

当我 运行 以上查询时,我得到这个结果:

a b c
1 43 65
2 66 333

当我 select 来自 tableY 的数据时,我得到这个:

a b c
1 54 65
2 88 567

tableY 数据是正确的数据,所以我想用 tableY 中的数据更新从第一个查询(带有 MINUS 子句的查询)返回的所有元组。

预期的结果,在更新子句之后,当我 select 来自 tableX 的数据应该是:

a b c
1 54 65
2 88 567

执行此 UPDATE 子句的最有效方法是什么?

update tablex
set (b, c) = (select b, c from tabley where tabley.a = tablex.a)
where exists (select 1 from tabley where tabley.a = tablex.a);

为了减小事务大小,添加 xb <> yb 或 xc <> yc:

update tablex
set (b, c) = (select b, c from tabley where tabley.a = tablex.a)
where exists (select 1 from tabley
              where tabley.a = tablex.a
                and (LNNVL(tabley.b = tablex.b) or LNNVL(tabley.c = tablex.c)));

在 oracle 上,我发现 MERGE 语法比 UPDATE 语法更有用...

MERGE INTO
  tablex   x
USING
  tabley   y
    ON (y.a = x.a)
WHEN MATCHED THEN UPDATE
  SET
    x.b = y.b,
    x.c = y.c
  WHERE
    LNNVL(x.b = y.b)
    OR
    LNNVL(x.c = y.c)

编辑:添加了 where 子句以避免冗余更新,在下面的评论之后。