Domanda

Sto cercando di utilizzare questa query in 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;
.

Ottengo questo errore:

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
.

Sono davvero confuso.La clausola con cui appare corretta per documentazione postgres.Se eseguo separatamente la query nella clausola con la clausola, ottengo risultati corretti.

È stato utile?

Soluzione

Dal Fine manuale :

.

Esistono due modi per modificare una tabella utilizzando le informazioni contenute in altre tabelle nel database: utilizzando le selezioni secondarie o specificando tabelle aggiuntive nella clausola FROM.

Quindi hai solo bisogno di A da clausola:

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;
.

Il messaggio di errore dice anche tanto:

.

Errore: mancata voce da-clausola per la tabella "Stop"

Altri suggerimenti

Questo può accadere anche se fulga un nome tabella.Ad esempio:

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

Invece di profiles I usato in modo errato profile !!Questo funzionerebbe:

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top