Postgresql 返回 Upsert 查询是否插入新行或更新行

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

我想知道 upsert/“冲突时更新”查询是否插入了新行或仅更新了一行。

有几种方法可以做到这一点,但感觉不正确 这是我当前的查询:

INSERT INTO mytable (id, mycolumn) VALUES (123, 'abc') ON CONFLICT DO UPDATE SET mycolumn='xyz' RETURNING (select max(id) from mytable)=id as inserted
sql postgresql upsert postgresql-9.5
1个回答
0
投票

这个问题出现在相关的页面上,似乎是正确的答案 使用系统列区分 UPSERT 中插入和更新的行

RETURNING (xmax = 0) AS inserted
© www.soinside.com 2019 - 2024. All rights reserved.