简体   繁体   English

使用WITH子句查询Postgres“缺少FROM子句条目”错误

[英]Postgres “missing FROM-clause entry” error on query with WITH clause

I am trying to use this query in Postgres 9.1.3: 我试图在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;

I get this error: 我收到此错误:

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

I'm really confused. 我真的很困惑。 The WITH clause appears correct per Postgres documentation. 根据Postgres文档,WITH子句显示正确。 If I separately run the query in the WITH clause, I get correct results. 如果我在WITH子句中单独运行查询,我会得到正确的结果。

From the fine manual : 精细手册

There are two ways to modify a table using information contained in other tables in the database: using sub-selects, or specifying additional tables in the FROM clause. 有两种方法可以使用数据库中其他表中包含的信息来修改表:使用子选择,或在FROM子句中指定其他表。

So you just need a FROM clause: 所以你只需要一个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;

The error message even says as much: 错误信息甚至说得多:

ERROR: missing FROM-clause entry for table "stops" 错误:缺少FROM-clause条目表“停止”

This can also happen if you mistype a table name. 如果您错误输入表名,也会发生这种情况。 For example: 例如:

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

Instead of profiles i incorrectly used profile !! 而不是profiles我错误地使用了profile This would work: 这可行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM