I'm working with some code. There are several queries whose effect is, if the row exists with some of the data filled in, then that 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 mimic this pattern and it seems to work, but I'm confused as to what is actually going on behind the scenes. Can anyone elucidate how this actually works? I'm using PostgreSQL.