Oracle SQL:在 UPDATE 中的 SELECT 的 WHERE 子句中使用别名

问题描述 投票:0回答:1

我需要使用其他表中的数据更新表中的数百行。该代码有效,但是否有可能不更改 id 和密钥并使用 id IN ('56454', '56546'...)

UPDATE kontakt ko
SET  ko.Strasse1 = (SELECT old FROM audit WHERE key = '962881' AND cname = 'STRASSE1' AND old IS NOT NULL)
    ,ko.plz = (SELECT old FROM audit WHERE key = '962881' AND cname = 'PLZ' AND old IS NOT NULL)
    ,ko.ort = (SELECT old FROM audit WHERE key = '962881' AND cname = 'ORT' AND old IS NOT NULL)
WHERE id = 962881;

COMMIT;

我尝试使用 AS 或替换名称,但没有成功。我找不到答案。

sql oracle sql-update where-clause
1个回答
0
投票

您希望使子查询相关,以便它们引用更新行的 ID:

UPDATE kontakt ko
SET  ko.Strasse1 = (SELECT old FROM audit WHERE key = TO_CHAR(ko.id) AND cname = 'STRASSE1' AND old IS NOT NULL)
    ,ko.plz = (SELECT old FROM audit WHERE key = TO_CHAR(ko.id) AND cname = 'PLZ' AND old IS NOT NULL)
    ,ko.ort = (SELECT old FROM audit WHERE key = TO_CHAR(ko.id) AND cname = 'ORT' AND old IS NOT NULL)
WHERE id IN ('56454', '56546'...);

COMMIT;
© www.soinside.com 2019 - 2024. All rights reserved.