简体   繁体   English

使用合并函数更新 PostgreSQL 中的表

[英]update the table in PostgreSQL with coalesce function

I am exploring this house data and there are some rows where there are 2 same data with different unique id but only one's address is missing so i wrote this code but i do not know where i am making the mistake.我正在探索这个房子数据,有些行有 2 个具有不同唯一 ID 的相同数据,但只有一个地址丢失,所以我写了这段代码,但我不知道我在哪里犯了错误。

UPDATE A
SET PROPERTYADDRESS = COALESCE(A.PROPERTYADDRESS,B.PROPERTYADDRESS)
FROM "House" A
JOIN "House" B ON A.UNIQUEID != B.UNIQUEID
AND A.PARCELID = B.PARCELID
WHERE A.PROPERTYADDRESS IS NULL

When i run this code i am getting this error当我运行此代码时,我收到此错误

ERROR:  relation "a" does not exist
LINE 1: UPDATE A

Postgres uses a different UPDATE syntax when you want to "join" some table than SQL Server for which your query is written (both are non-standard).当您想要“加入”某个表而不是为其编写查询的 SQL Server(两者都是非标准的)时,Postgres 使用不同的 UPDATE 语法。

The target table should not be repeated in the FROM clause unless you want a self join.除非您想要自联接,否则不应在 FROM 子句中重复目标表。 But then you should only put it into the FROM clause but not "join" it into the result a third time through a JOIN operator.但是,您应该只将它放入 FROM 子句中,而不是第三次通过 JOIN 运算符将其“加入”到结果中。

As you only select rows from the target table where propertyaddress is NULL, the coalesce is not necessary.由于您仅从目标表中选择propertyaddress为 NULL 的行,因此不需要合并。

So you probably want:所以你可能想要:

UPDATE "House" A
  SET PROPERTYADDRESS = B.PROPERTYADDRESS
FROM "House" B 
WHERE A.UNIQUEID != B.UNIQUEID
  AND A.PARCELID = B.PARCELID
  AND A.PROPERTYADDRESS IS NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM