I am trying to write an UPDATE statement to update a column in a table based on multiple WHERE conditions. See query below,
UPDATE table_new
SET col_4 = 'new value'
WHERE ?
(SELECT col_1, col_2, col_3 FROM table_new
EXCEPT
SELECT col_1, col_2, col_3 FROM table_old);
I trying to update col_4 with the new value for the unique col_1 + col_2 + col_3 combination coming from the EXPECT SQL query. I am not sure about what would follow the WHERE condition as the WHERE clause is usually followed by just one column in an UPDATE statement. I am thinking of doing a CONCAT for the unique col_1 + col_2 + col_3 combination for both the EXPECT SQL query and the column name that would follow the WHERE clause but not sure if that would help my case
My possible solution:
UPDATE table_new
SET col_4 = 'new value'
WHERE CONCAT('col_1','-','col_2','-','col_3') IN
(SELECT CONCAT('col_1','-','col_2','-','col_3') FROM table_new
EXCEPT
SELECT CONCAT('col_1','-','col_2','-','col_3') FROM table_old
);
Sample Data in table_new (Before running the UPDATE statement):
Col_1 Col_2 Col_3 Col_4 (old value)
123456 123XYZ 456ABC 100
654321 ZYX321 CBA654 200
Desired Result in table_new (After running the UPDATE statement):
Col_1 Col_2 Col_3 Col_4 (new value)
123456 123XYZ 456ABC 300
654321 ZYX321 CBA654 400