选择使用WITH

时间:2016-05-30 16:40:25

标签: postgresql

我有

with
s1 as (select serial from wingstatushref where href = ? for key share),
i1 as (insert into wingstatushref
select ?
where not exists(select * from s1)
on conflict (href) do update set href=? returning serial)

使用相同的?三次。我尝试整合了?进入了:

with
h as (values (?)),
s1 as (select serial from wingstatushref where href=h for key share),
i1 as (insert into wingstatushref
select h
where not exists(select * from s1)
on conflict (href) do update set href=h returning serial)
select serial from s1 union all select serial from i1;

但那给了我

x SQL Compiles and Typechecks
 x ERROR: column "h" does not exist
  Position: 83 (specs2.scala:64)

从select子句引用h中数据的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

感谢来自#postgresql的RhodiumToad

with
  h(hr) as (values (?)),
  s1 as (select serial from h, wingstatushref where href=h.hr for key share),
  i1 as (insert into wingstatushref
    select hr from h
    where not exists(select * from s1)
    on conflict (href) do update set href=wingstatushref.href where EXCLUDED.href=wingstatushref.href returning serial)
  select serial from s1 union all select serial from i1;