【问题标题】:Update from existing table in Redshift从 Redshift 中的现有表更新
【发布时间】:2015-10-06 08:50:31
【问题描述】:

我想根据其他表的结果更新 Redshift 表中的值,我正在尝试运行以下查询但收到错误。

update section_translate
set word=t.section_type
from (
select distinct section_type from mr_usage where section_type like '%sディスコ')t
where word = '80sディスコ'

我收到的错误:

ERROR: Target table must be part of an equijoin predicate

无法理解我的查询中有什么不正确的地方。

【问题讨论】:

  • 子查询必须产生恰好一个结果行(每个目标行)。您的可能产生多个结果行。
  • 查询只收到一个结果
  • 您是否单独运行了子查询? (select distinct section_type from mr_usage where section_type like '%sディスコ'; ) 此外,它与主查询无关。最好的测试方法是用相应的SELECT x.*, z ... 临时替换UPDATE x set y = z ...,看看会是什么结果。
  • 仅供参考,此文档页面上引用了有关 OUTER joins 的错误(但这似乎不是这里的情况):docs.aws.amazon.com/redshift/latest/dg/…

标签: postgresql amazon-redshift


【解决方案1】:

您需要将不相关的子查询变成相关的子查询,

update section_translate
set word=t.section_type
from (
select distinct section_type,'80sディスコ' as word  from mr_usage where section_type like '%sディスコ')t
where section_translate.word = t.word

否则,外部查询的每条记录都符合更新条件,查询引擎将拒绝它。 Postgre(以及 Redshift)评估不相关子查询的方式与 SQL Server/Oracle 等略有不同。

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2016-12-11
    相关资源
    最近更新 更多