gpt4 book ai didi

postgresql - 如何在 ON CONFLICT DO UPDATE SET X = OLD.X + EXCLUDED.X 中引用 OLD.X

转载 作者:行者123 更新时间:2023-11-29 11:44:58 24 4
gpt4 key购买 nike

ON CONFLICT DO UPDATE 中有一个“旧值”的别名?


我的现实生活问题是

INSERT INTO art.validterm (namespace,term,X,info)
SELECT namespace,term,array_agg(Xi), 'etc'
FROM term_raw_Xs
GROUP BY namespace,term
ON CONFLICT (term) DO
UPDATE SET aliases=OLD.X||EXCLUDED.X
WHERE term=EXCLUDED.term

PS:问题是不存在“OLD”。解析器说只有 X 是不明确的。

最佳答案

简单地将 OLD 替换为表的名称,在您的情况下:validterm,对我有用。

我的测试:

DROP TABLE IF EXISTS work.term_raw;

CREATE TABLE work.term_raw
(
unique_field INT UNIQUE,
x_field TEXT
);

INSERT INTO work.term_raw VALUES (1, 'A');

INSERT INTO work.term_raw VALUES (1, 'B')
ON CONFLICT (unique_field) DO UPDATE SET x_field = term_raw.x_field || EXCLUDED.x_field;

SELECT * FROM work.term_raw;

我的结果:

enter image description here

关于postgresql - 如何在 ON CONFLICT DO UPDATE SET X = OLD.X + EXCLUDED.X 中引用 OLD.X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38704991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com