可能重複:
Insert, on duplicate update (postgresql)
Cannot SELECT from UPDATE RETURNING clause in postgresPostgreSQL的UPSERT查詢
幫助理解我的語法錯誤。我儘量讓這樣的實施UPSERT查詢的 PosgreSql:
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
但更新的子查詢,而不嵌件工作做好。
任何最簡單的方法來做upsert查詢?
不,這不是重複 – potapuff