【问题标题】:insert select from on duplicate key ignore postgres在重复键上插入选择忽略postgres
【发布时间】:2017-06-19 02:07:46
【问题描述】:

我从这个来源读到,您可以在 postgres 中对重复键忽略进行插入,但我似乎无法让它适用于 select from:

link

我看到你能做的是:

insert into tab(id, val) values(1, 'nosert'), (2,
'nosert'), (3, 'nosert') on duplicate key ignore returning id;

为什么我不能这样做?

insert into table_one (field_one, field_two) select field_one, field_two from table_two on duplicate key ignore returning field_one;

我被告知我在重复附近有语法错误,但之前的查询运行良好(尽管它在重复索引上发生冲突)并且查询的其余部分只是添加了 on duplicate key ignore returning field_one

select from 不可能吗?

【问题讨论】:

    标签: postgresql on-duplicate-key insert-select


    【解决方案1】:

    能够使用on conflict do nothing 解决 9.5

    insert into table_one (field_one, field_two) 
    select field_one, field_two from table_two 
    on conflict do nothing;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-19
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 2012-11-27
      • 2017-10-16
      • 1970-01-01
      相关资源
      最近更新 更多