Skip to content
Advertisement

Replace Database field based on 2 different Table Conditions

I need to update the table based on email matching condition and need to know SQL query that can solve this problem.

I have 2 tables “old_data” and “new_data”

Old_data contains around 60,000 records and fields "EMAIL"
New_data contains around 90,000 records and fields "EMAIL" "WHITE_NO"

Here is my logical condition I need help with.

Update * from Old_data, New_data SET New_data.WHITE_NO = 'true' WHERE old_data.EMAIL = New_data.EMAIL

What it will do is look for all emails in the old_data table and match them with all same emails into the new_data table and replace the WHITE_NO field values to true.

Advertisement

Answer

UPDATE new_data
       JOIN old_data
       ON new_data.EMAIL = old_data.EMAIL
SET    new_data.WHITE_NO = 'true';

OR

UPDATE new_data
SET new_data.WHITE_NO ='true'
WHERE new_data.EMAIL IN (SELECT EMAIL FROM old_data);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement