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

问题描述

在20/07/20时如何使用“已修改”(t2)更新“ date_from”(t1)。

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

表1:

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

表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'
             )
,

您预先知道需要分配哪个值,因此您只需要过滤应更新的行。 __repr__()似乎足够:

exists
,

如果速度很重要,

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);

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...