我有一个独特的专栏。我想插入一行(如果它还没有),然后返回该行的id
。
INSERT INTO t(a) VALUES ('a') ON CONFLICT DO NOTHING RETURNING t.id;
答案 0 :(得分:2)
with i as (
INSERT INTO t(a) VALUES ('a') ON CONFLICT (a) DO NOTHING RETURNING id
)
select id from i
union all
select id from t where a = 'a'
limit 1