【发布时间】:2016-12-25 03:32:57
【问题描述】:
我正在尝试使用“WITH”在 Postgres 9.5 中进行不安查询
with s as (
select id
from products
where product_key = 'test123'
), i as (
insert into products (product_key, count_parts)
select 'test123', 33
where not exists (select 1 from s)
returning id
)
update products
set product_key='test123', count_parts=33
where id = (select id from s)
returning id
显然,我只在更新时检索 id,即使我知道插入成功,也没有在插入时得到任何结果。
我需要修改此查询,以便在插入和更新时都能获取 id。
谢谢!
【问题讨论】:
-
在另一个 CTE 中更新,然后在最终选择中检索 ID。
标签: sql postgresql postgresql-9.5