Firebird 使用其他 table 的条件更新字段

Firebird UPDATE a field using conditions of other table

我在 Firebird 中有两个表,其中包含以下字段:

TABLEA: KEY, ITEM, LAST_SALE_DATE 

TABLEB: KEY, ROT

两者有键相关。他们使用关键字段引用相同的项目。

我要更新TABLEB.ROT='A' where TABLEA.LAST_SALE_DATE>01/01/2016

不知道怎么写,求大神指教

有几种方法可以做到这一点。首先,您可以通过对 TABLEA:

进行存在性检查来关联 TABLEBupdate
update TABLEB b set b.ROT = 'A' 
where exists (
  select 1 
  from TABLEA a 
  where a.KEY = b.KEY and a.LAST_SALE_DATE > DATE '2016-01-01')

另一种方法是使用 MERGE:

merge into TABLEB b
  using TABLEA a
    on a.KEY = b.KEY and a.LAST_SALE_DATE > DATE '2016-01-01' 
  when matched then
    update set b.ROT = 'A'