如何在冲突时更新 select 的确切行?
How update the exact row of the select, on conflict?
我只需要在冲突时更新,并且所有选中的行
INSERT INTO t SELECT * FROM fdw_t
ON CONFLICT (id) DO UPDATE
SET id=id, etc1=etc1, etc2=etc2
; -- ERROR: column reference "id" is ambiguous
INSERT INTO t SELECT * FROM fdw_t AS w
ON CONFLICT (id) DO UPDATE
SET id=w.id, etc1=w.etc1, etc2=etc2
; -- ERROR: missing FROM-clause entry for table "w"
-- BUT I NEED the ROW of the select, not other!
您可以像使用 VALUES
子句执行 INSERT
时那样引用 EXCLUDED
行:
INSERT INTO t
SELECT * FROM fdw_t
ON CONFLICT (id)
DO UPDATE
SET id = EXCLUDED.id, etc1 = EXCLUDED.etc1, etc2 = EXCLUDED.etc2;
我只需要在冲突时更新,并且所有选中的行
INSERT INTO t SELECT * FROM fdw_t
ON CONFLICT (id) DO UPDATE
SET id=id, etc1=etc1, etc2=etc2
; -- ERROR: column reference "id" is ambiguous
INSERT INTO t SELECT * FROM fdw_t AS w
ON CONFLICT (id) DO UPDATE
SET id=w.id, etc1=w.etc1, etc2=etc2
; -- ERROR: missing FROM-clause entry for table "w"
-- BUT I NEED the ROW of the select, not other!
您可以像使用 VALUES
子句执行 INSERT
时那样引用 EXCLUDED
行:
INSERT INTO t
SELECT * FROM fdw_t
ON CONFLICT (id)
DO UPDATE
SET id = EXCLUDED.id, etc1 = EXCLUDED.etc1, etc2 = EXCLUDED.etc2;