关于postgis:postgreSQL 有表\\”xxx\\”的条目,但是从这部分查询中不能引用

postgreSQL There is an entry for table "xxx", but it cannot be referenced from this part of the query

postgreSQL 中编写此查询的正确方法是什么?
我正在尝试规范化(即标准化)地址。如果我给它一个硬编码的地址,我可以毫无问题地使用 pagc 。但是,我需要为它提供一个从部分解析的地址。我看到这里有几个关于堆栈溢出的类似问题引用了相同的错误。这些查询很复杂,而且都与我的完全不同,所以我无法通过阅读其他帖子找到解决方案。

我试过了:

1
WITH full_address AS (home_address1 || ','|| home_city ||','|| home_state ||,','|| home_zip) UPDATE contacts SET (home_house_num, home_predirection, home_street_name, home_street_type,home_postdirection, home_unit_num) = (addy.address_alphanumeric,addy.predirabbrev,addy.streetname, addy.streettypeabbrev,addy.postdirabbrev,addy.internal) FROM pagc_normalize_address(full_address) AS addy WHERE contact_id = 833826;

这会引发错误:

syntax error at or near"home_address1"
LINE 26: with full_address as (home_address1 || ','|| home_city |.

我也试过了:

1
UPDATE contacts SET (home_house_num, home_predirection, home_street_name, home_street_type,home_postdirection, home_unit_num) = (addy.address_alphanumeric,addy.predirabbrev,addy.streetname, addy.streettypeabbrev,addy.postdirabbrev,addy.internal) FROM pagc_normalize_address(home_address1 ||','||home_city||','||home_state||','||','||home_zip) AS addy WHERE contact_id = 833826;

错误:

ERROR: invalid reference to FROM-clause entry for table"contacts"
LINE 24: ...abbrev,addy.internal) FROM pagc_normalize_address(home_addre...
^
HINT: There is an entry for table"contacts", but it cannot be referenced from this part of the query.
SQL state: 42P10
Character: 2297


第一个查询是胡言乱语,第二个是有意义的但失败了,因为您不能在 FROM 子句中使用对更新表的列的横向引用。

试试这样的 CTE:

1
2
3
4
5
6
7
8
9
10
11
12
13
WITH addy AS (
   SELECT addy.* FROM
      contacts
      CROSS JOIN LATERAL
      pagc_normalize_address(home_address1
 || ',' || home_city || ',' || home_state || ',' || ',' || home_zip) AS addy
   WHERE contacts.contact_id = 833826
)
UPDATE contacts
SET (home_house_num, home_predirection, home_street_name, home_street_type,home_postdirection, home_unit_num)
    = (addy.address_alphanumeric,addy.predirabbrev,addy.streetname, addy.streettypeabbrev,addy.postdirabbrev,addy.internal)
FROM addy
WHERE contact_id = 833826;