gpt4 book ai didi

sql - 使用 WITH 子句查询时出现 Postgres "missing FROM-clause entry"错误

转载 作者:行者123 更新时间:2023-11-29 11:08:44 24 4
gpt4 key购买 nike

我正在尝试在 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 子句中单独运行查询,我会得到正确的结果。

最佳答案

来自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"

关于sql - 使用 WITH 子句查询时出现 Postgres "missing FROM-clause entry"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9643859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com