重複の可能性:
挿入、重複更新時(postgresql)
postgresのUPDATERETURNING句から選択できません
構文エラーを理解するのに役立ちます。私はPosgreSqlでupsertクエリのそのような実装をしようとしています:
create table tbl( key int, val int);
insert into tbl(key,val)
select distinct(key), 0 from unnest('{0,1,2,3,4,5}'::int[]) as key
where key not in (
update tbl set val = 1
where key = any('{0,1,2,3,4,5}'::int[])
returning key
);
エラーは:
ERROR: syntax error at or near "tbl"
ROWS 6: update tbl set val = 1
^
********** Error **********
ERROR: syntax error at or near "tbl"
SQL state: 42601
Character: 167
ただし、部分を挿入せずにサブクエリを更新するとうまく機能します。
アップサートクエリを作成する最も簡単な方法はありますか?