2012-03-10 8 views
22

WITH持つクエリでのエラー "FROM句エントリを行方不明":Postgresの私は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 

I本当に混乱しています。 WITH句は、Postgresのドキュメントごとに正しく表示されます。 WITH句で別々にクエリを実行すると、正しい結果が得られます。 fine manualから

+0

おっと!ありがとう。診断のステップとして**停止**テーブルの名前を変更しようとしましたが、それは明らかに問題ではありません。 –

答えて

28

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

あなたは正しいです!彼らはここに「骨頭」のポイントを与える必要があります。私はちょうど1つを獲得したと思う。 –

+3

@ArenCambre:そういうことがあったら、私たちはすべて「骨頭」のポイントを超えていると思う:)最も目に見える問題のいくつかは、あなたの顔の正面にあるものである。 –

+2

非常に良い答え。ちょっとした質問です:FROM stopではなくFROM stopでなければなりません。 – joragupra

1

場合、これがまた起こることができますあなたはテーブル名を誤って入力します。たとえば:

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

代わりのprofiles私が間違ってprofileを使用しました!これは動作します:

UPDATE profiles SET name = (profiles.first_name) WHERE id = 1 
関連する問題