Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert select from on duplicate key ignore postgres

I've read from this source that you can do an insert on dupcliate key ignore in postgres, but I cannot seem to get this to work for a select from:

link

What I've seen you can do is:

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

Why can't I do something like this?

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

I'm being told that I have syntax error near duplicate, but the query before then runs fine (although it colides on a duplicate index) and the rest of the query just adds the on duplicate key ignore returning field_one.

Is this not possible with select from?

like image 557
Alexander Kleinhans Avatar asked Feb 02 '17 05:02

Alexander Kleinhans


1 Answers

Was able to resolve with on conflict do nothing for 9.5

insert into table_one (field_one, field_two) 
select field_one, field_two from table_two 
on conflict do nothing;
like image 133
Alexander Kleinhans Avatar answered Oct 19 '22 09:10

Alexander Kleinhans