Postgres upsert using results from select

为君一笑 提交于 2019-12-07 03:35:53

问题


I'm trying to upsert with postgres using values from a select. It looks like:

INSERT INTO foo (a, b, c)
SELECT a_, b_, c_
-- hairy sql
ON CONFLICT (...condition...) DO UPDATE
SET "c"=???

On conflict, I want to use one of the values from my select statement, but I can't find the right syntax to alias it. How can I do this with Postgres?


回答1:


Use the excluded keyword:

INSERT INTO foo (a, b, c)
SELECT a_, b_, c_
-- hairy sql
ON CONFLICT (...condition...) DO UPDATE
  SET c = excluded.c;


来源:https://stackoverflow.com/questions/44466232/postgres-upsert-using-results-from-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!