postgres在查询时出现“缺少 - 子句缺失”,其中包含子句
-
10-12-2019 - |
题
我正在尝试在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文档都有正确的条款。如果我在用子句中单独运行查询,我得到了正确的结果。
解决方案
从精细手册:
有两种方法可以使用数据库中其他表中包含的信息修改表:使用子选项,或在
FROM
子句中指定其他表。所以你只需要一个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;
错误消息甚至说:
错误:表中缺少表“停止”
其他提示
如果您错过了表名,也会发生这种情况。例如:
UPDATE profiles SET name = ( profile.first_name ) WHERE id = 1
.
而不是profiles
i错误地使用的profile
!!这将有效:
UPDATE profiles SET name = ( profiles.first_name ) WHERE id = 1
. 不隶属于 StackOverflow