【问题标题】:Postgres "missing FROM-clause entry" error on query with WITH clause带有 WITH 子句的查询时出现 Postgres“缺少 FROM 子句条目”错误
【发布时间】:2012-03-10 04:10:16
【问题描述】:

我正在尝试在 Postgres 9.1.3 中使用此查询:

WITH stops AS (
    SELECT citation_id,
           rank() OVER (ORDER BY offense_timestamp,
                     defendant_dl,
                     offense_street_number,
                     offense_street_name) AS stop
    FROM   consistent.master
    WHERE  citing_jurisdiction=1
)

UPDATE consistent.master
SET arrest_id = stops.stop
WHERE citing_jurisdiction=1
  AND stops.citation_id = consistent.master.citation_id;

我收到此错误:

ERROR:  missing FROM-clause entry for table "stops"
LINE 12: SET arrest_id = stops.stop
                         ^

********** Error **********

ERROR: missing FROM-clause entry for table "stops"
SQL state: 42P01
Character: 280

我真的很困惑。根据 Postgres 文档, WITH 子句似乎是正确的。如果我在 WITH 子句中单独运行查询,我会得到正确的结果。

【问题讨论】:

  • 哎呀!谢谢。我想说我尝试将 stops 表重命名为诊断步骤,但这显然不是问题。

标签: sql postgresql with-clause


【解决方案1】:

来自fine manual

使用数据库中其他表中包含的信息修改表有两种方法:使用子选择,或在FROM 子句中指定附加表。

所以你只需要一个 FROM 子句:

WITH stops AS (
    -- ...
)
UPDATE consistent.master
SET arrest_id = stops.stop
FROM stops -- <----------------------------- You missed this
WHERE citing_jurisdiction=1
  AND stops.citation_id = consistent.master.citation_id;

错误消息甚至说了这么多:

错误:缺少表“停止”的 FROM 子句条目

【讨论】:

  • 你是对的!他们需要在这里给出“白痴”积分。我想我刚赚了一个。
  • @ArenCambre:我认为如果有这样的事情,我们都会有过多的“愚蠢”点 :) 一些最难看到的问题就是摆在你面前的问题。
  • 非常好的答案。只是一个小问题:应该是 FROM stop 而不是 FROM stop,对吧?
  • @joragupra 是的,应该是(现在是)stops,谢谢。
【解决方案2】:

如果您输入错误的表名,也会发生这种情况。例如:

UPDATE profiles SET name = ( profile.first_name ) WHERE id = 1

而不是profiles 我错误地使用了profile !!这会起作用:

UPDATE profiles SET name = ( profiles.first_name ) WHERE id = 1

【讨论】:

    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多