Table 1 –
col1 | col2 |
---|---|
1 | 443 |
2 | 449 |
3 | 479 |
Table 2 –
col1 | col2 |
---|---|
1 | NULL |
2 | NULL |
3 | NULL |
Note - col1 is not a PK
expected Output –
Table 2 –
col1 | col2 |
---|---|
1 | 443 |
2 | 449 |
3 | 479 |
This is my python code:
abc_df = pd.read_sql_query("UPDATE Table1 SET Table1_id = t2.col1 FROM Table1 t1
INNER JOIN Table2 t2 ON t1.col2 = t2.col2", con=connection)
But My Actual OUTPUT is –
Table 2 –
col1 | col2 |
---|---|
1 | 443 |
2 | 443 |
3 | 443 |
3
Answers
You need to update your UPDATE query –
in your update query there is 2 part are wrong first one where set the table2 data, you used Table1_id = t2.col1 but you should used t2.col2 = t1.col2 and other wrong thing in join condition you used t1.col2 = t2.col2 but you should used t1.col1 = t2.col1
update your query as per following
The only thing I will add to existing answers is in general updates can be expensive operations, so put in a check to be sure you NEED to update it before you do it.
Because of MVCC, it will actually create a new record and create a dead space where the previous record was (until the next vacuum), so it makes senses on many levels to not update a record if all you are doing is replacing the value.
I realize in your example you were replacing all nulls, but in general this is just a good practice.