I am working with some code. There are several queries whose effect, if a row exists with some filled data, then this row is updated with the rest of the data, and if the row does not exist, a new one is created. They look like this:
INSERT INTO table_name (col1, col2, col3)
SELECT% s AS COL1,% s AS COL2,% s AS COL3
FROM (SELECT% s AS COL1,% s AS COL2,% s AS COL3) A
LEFT JOIN table_name B
ON B.COL1 =% s
AND B.COL2 =% s --note: doesn't mention all columns here
WHERE B.id IS NULL
LIMIT 1
I can imitate this pattern and it seems to work, but I'm confused about what is actually going on behind the scenes. Can anyone figure out how this works? I am using PostgreSQL.
sql postgresql insert-update
Claudiu
source share