Try to update a table with a new set of values,
if the values already exist then it should update the fields, if it doesn't then it should insert the fields.
WITH f AS (
SELECT 1 as MarketId,
'dros@test.com' as userName,
28 as age,
1 as isPremiumMember,
1 as isSubscribed,
'2021-03-12T17:07:30' as LastModifiedOn
from members
)
INSERT INTO members
(age,isPremiumMember,isSubscribed, lastModifiedOn)
VALUES (f.age,f.isPremiumMember,f.isSubscribed,f.lastModifiedOn)
ON CONFLICT (age,isPremiumMember,isSubscribed,lastModifiedOn)
DO UPDATE SET age= EXCLUDED.age,isPremiumMember = EXCLUDED.isPremiumMember,isSubscribed= EXCLUDED.isSubscribed,lastModifiedOn= EXCLUDED.lastModifiedOn;
however when I run the query I get this error:
missing FROM-clause entry for table "f"
trying to write this in plain SQL
INSERT INTO members (age,isPremiumMember,isSubscribed, lastModifiedOn) select age,isPremiumMember,isSubscribed,lastModifiedOn from f ON CONFLICT (age,isPremiumMember,isSubscribed,lastModifiedOn) DO UPDATE SET age= EXCLUDED.age,isPremiumMember = EXCLUDED.isPremiumMember,isSubscribed= EXCLUDED.isSubscribed,lastModifiedOn= EXCLUDED.lastModifiedOn;
tried adding the unique constraint as select as recommend below. this is my current statement.