【问题标题】:How to update table's column value in Redshift based on a join?如何根据联接更新 Redshift 中的表列值?
【发布时间】:2020-03-13 16:40:33
【问题描述】:

如何在 Redshift 中使用此值更新此表:

UPDATE t1
SET col1 = 'new_value_here'
FROM t1
    LEFT JOIN t2
        on t1.col2 = t2.col2

WHERE
t1.country IN ('USA', 'JAPAN')
    AND t1.col1 = 'old_value_here'
    AND t2.col2 IS NULL;

我收到错误:“目标表必须是等值连接谓词的一部分;”

【问题讨论】:

  • 有人可以帮忙吗?

标签: join sql-update amazon-redshift amazon-redshift-spectrum


【解决方案1】:

Redshift 不允许在 UPDATE 查询的 FROM 子句中使用任何 OUTER JOINS。您需要将其用作子查询或重新编写逻辑。

这是我对您的查询所做的,但随着您对数据的更好理解,您可能希望以您的方式编写它。

UPDATE t1
SET col1 = 'new_value_here'
from (select t1.col2 as t1_col2, t2.col2 as t2_col2 
      from t1 left join t2 on t1.col2=t2.col2) t1t2
WHERE t1t2.t1_col2=t1.col2
    and t1t2.t2_col2 is null
    and t1.country IN ('USA', 'JAPAN')
    AND t1.col1 = 'old_value_here';

【讨论】:

    猜你喜欢
    • 2016-11-11
    • 2013-08-16
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多