Skip to content
Advertisement

insert/update data from one column to another with on conflict update, only those that have different values for specific fields?

I have two columns products/tmp_products with following specification
(id bigint primary key ,price int not null,quantity int not null ,lastupdate timestamp)
I want sql to
                  Insert from tmp_products to products when id not in products.
                  Update only if (price or quantity) values are changed.

I’m getting following error
column reference "price" is ambiguous
I want to know how to approach this problem?

Advertisement

Answer

The problem is in the where clause. You need to qualify the columns so there are unambiguous:

Note that you also need to remove the parentheses around the SELECT, otherwise your query is not valid Postgres SQL, and would raise error INSERT has more target columns than expressions.

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