根据唯一标识符在 SQL 中使用临时表更新当前表



我有一个主表

id date           in_US?
1  '2020-06-15'   FALSE
2  '2020-06-20'   TRUE
3  '2020-06-22'   FALSE
4  '2020-06-25'   TRUE

然后是更新表

id date           in_US?
1  '2020-06-15'   TRUE
2  '2020-06-20'   FALSE

如何编写一个更新语句,根据唯一标识符 (id( 使用更新表中的行更新主表?

在 Redshift 中,您将使用update ... set ... from ... where语法,如下所示:

update main_table
set in_us = u.in_us, date = u.date
from update_table u
where main_table.id = u.id

我认为你可以做到:

update maintable m
set in_us = u.is_us
from updatetable u
where m.id = u.id;

我不知道你是否也想考虑日期。 如果是这样,这也将列入where条款。

最新更新