I’m curious if I can update all matching fields in a row with a new value at once in MySQL.
I know I can use ALTER TABLE to change default values, but I’m interested only in one row, not in changing the default values of an entire table. I can iterate through each in a loop if necessary, but I’m curious if there is a way to accomplish this more elegantly.
Imagine my row looks like this:
| 1 | Joe | Smith | null | X | X | X | Y | X | Y | Y | X | null | Y | X | null |
Is it possible to update all fields matching X to Y at once?
So the result would look like this:
| 1 | Joe | Smith | null | Y | Y | Y | Y | Y | Y | Y | Y | null | Y | Y | null |
(assume 1 is a primary key, id… if that helps at all)
Can this be done with only one UPDATE query?
This will change the value of all columns to ‘y’ if they are ‘x’, otherwise leave it unchanged.
Actually, it updates it to the original value otherwise, but semantically that’s the same as leaving it unchanged, and in mysql is identical (ie it doesn’t count as an “update” in any way)