I'm having an issue using the CASE statement in MySql. I added some code below as an example of what I'm trying to do. I thought that if there are no matches in the WHEN statement that no changes will occur, but that doesn't seem to happen.
I don't have a record that has a value 66 for contact_id in the my_contacts table, so I figured nothing will happen, but instead all the values in the zip_code column change to null. Why is this?
UPDATE my_contacts
SET zip_code =
CASE
WHEN contact_id = 66 THEN '33333'
END;
How Do i only update a few records using case? For instance, I want to update only records that match contact_id = 1 and contact_id =2. It does update those two records, but also changes an existing zip code from '90004' to NULL, Why is that?
UPDATE my_contacts
SET zip_code =
CASE
WHEN contact_id = 1 THEN '94301'
WHEN contact_id = 2 THEN '08540'
END;
Query OK, 3 rows affected (0.01 sec) Rows matched: 5 Changed: 3 Warnings: 0