Oracle - 使用第二个 table 中的行更新第一个 table 中的数据

Oracle - update data in first table with rows from second table

如何在像 20/07/20 时使用 'modfied' (t2) 更新 'date_from' (t1)。

因此在这种情况下,t1 中的 id 1 和 2 将被更新,而 id 3 保持不变。

Table 1:

id    date_from
-----------------------
1     13/07/30
2     13/07/30
3     13/07/30

Table 2:

id    name    modified
-----------------------
1     x       20/07/20
2     y       20/07/20
3     z       19/05/10

像这样:

update t1 a set
  a.date_from = (select b.modified
                 from t2 b
                 where b.id = a.id
                   and b.modified = date '2020-07-20'
                )
where exists (select null
              from t2 c
              where c.id = a.id
                and c.modified = date '2020-07-20'
             )

您事先知道需要分配哪个值,因此您只需要过滤应该更新的行即可。 exists 似乎足够了:

update t1 
set date_from = date '2020-07-20'
where exists (
    select 1 from t2 where t2.id = t1.id and t2.modified = date '2020-07-20'
)

如果速度很重要,

merge into t1 trg
using 
(
    select  id, modified
    from    t2
    where   modified = date'2020-07-20'
) src
on ( trg.id = src.id )
when matched then update
set trg.date_from = src.modified
where lnnvl(trg.date_from = src.modified);