从子查询更新多列

Update multiple columns from a sub query

UPDATE PINPOINT_SUPPLEMENT
            SET (ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (
            SELECT key,'file'
            FROM PINPOINT_DOCUMENT
            WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
            )
WHERE ATTACHMENT_VALUE IS NULL

执行此查询时出现错误

错误:"SELECT" 处或附近的语法错误 第 3 行:SELECT 键,'文件

update PINPOINT_SUPPLEMENT
set
    ATTACHMENT_VALUE = PINPOINT_DOCUMENT.key,
    ATTACHMENT_TYPE = 'file'
from PINPOINT_DOCUMENT
where
    PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
    and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL

update PINPOINT_SUPPLEMENT
set
    (ATTACHMENT_VALUE,ATTACHMENT_TYPE) = (PINPOINT_DOCUMENT.key, 'file')
from PINPOINT_DOCUMENT
where
    PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID
    and PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE IS NULL

Postgres 9.5 中引入了更新元组的支持,因此您不能在您的版本中使用该语法。

但是由于第二个值是一个常量,我看不出有理由使用该语法开头:

UPDATE PINPOINT_SUPPLEMENT
  SET ATTACHMENT_VALUE = (SELECT "key"
                          FROM PINPOINT_DOCUMENT
                          WHERE PINPOINT_SUPPLEMENT.ATTACHMENT_VALUE::integer = PINPOINT_DOCUMENT.DOCUMENT_ID), 
       ATTACHMENT_TYPE = 'file'
WHERE ATTACHMENT_VALUE IS NULL

请注意,如果 document_id!

有多个 ATTACHMENT_VALUE,子查询可能会导致错误