postgres returning id, insert or select if id is present or not

偶尔善良 提交于 2019-12-25 07:27:03

问题


WITH x AS (
   INSERT INTO users(name, age)
    SELECT 'john', 30
    WHERE NOT EXISTS (
       SELECT id FROM users WHERE name = 'john' 
    ) u
    RETURNING (CASE WHEN u.id > 0 THEN u.id ELSE id END)
)

SELECT * FROM x

How can I always return an id and if the id is not present, then insert it. Also, I need this to be in a CTE.

来源:https://stackoverflow.com/questions/24050779/postgres-returning-id-insert-or-select-if-id-is-present-or-not

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