Skip to content
Advertisement

How to specify information from the table being inserted on PostgreSQL upsert?

I am trying to insert values from a temporary table (NewThings) with columns Thing and Counts into my main table (MyThings) with columns Thing and Count. If a thing does not exist in the main table I want to insert a new row, otherwise I want to add to the counter. However, I am struggling with the correct syntax on the update statement. This is what I would like to do:

But I only get this to work:

How can I correctly add the counts value from the NewThings table?

Advertisement

Answer

You have to use EXCLUDED instead of NewThings

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement