2012-03-10 63 views
22

我想在Postgres的9.1.3使用此查询:Postgres的“失踪FROM子句条目”上查询错误与条款

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 

我真的很困惑。 WITH子句在每个Postgres文档中显示正确。如果我单独在WITH子句中运行查询,我会得到正确的结果。

+0

哎呀!谢谢。我想说我尝试将**停止**表格改名为诊断步骤,但这显然不是问题。 – 2012-03-10 04:38:32

回答

28

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子句:

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; 

错误消息甚至说,随着多:

ERROR: missing FROM-clause entry for table "stops"

+3

你是对的!他们需要在这里给出“骨头”的观点。我想我只是赢得了一个。 – 2012-03-10 04:34:51

+3

@ArenCambre:我认为如果有这样的事情,我们都会有过多的“骨头”要点:)看到的一些最难的问题就是那些在你面前的问题。 – 2012-03-10 04:49:00

+2

非常好的答案。只是一个小问题:它应该是从停止而不是停止,对吧? – joragupra 2015-10-27 11:48:26

1

这也可能发生,如果你错误地输入了一个表名。例如:

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

代替profiles我使用不当profile !!这将工作:

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