postgreSQL There is an entry for table "xxx", but it cannot be referenced from this part of the query
在
我正在尝试规范化(即标准化)地址。如果我给它一个硬编码的地址,我可以毫无问题地使用
我试过了:
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
第一个查询是胡言乱语,第二个是有意义的但失败了,因为您不能在
试试这样的 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; |