I have an SQL UPDATE statement using the compound primary key (Key1,Key2) of a table that looks something like this:
UPDATE TableName SET FieldName = CASE
WHEN (Key1=389 AND Key2=5594091315209354374) THEN 1320243147187
WHEN (Key1=397 AND Key2=8686441440518828409) THEN 1320243147562
WHEN (Key1=389 AND Key2=5717973625907258381) THEN 1320243147182
....
WHEN (Key1=394 AND Key2=5512452777552926025) THEN 1320243147389 END
WHERE Key2 IN (123782199165241826,5594091315209354374,...,3553840348728167644)
AND Key1 IN (400,394,391,389,397);
I might have, say, 20 or so WHENs in the CASE statement.
How might MySQL say this has updated slightly more rows than there are WHENs?
Your CASE statement has nothing to do with which rows are affected. It’s entirely up to your WHERE clause:
Every row that fits those conditions will be updated. The question, then, is what happens to the rows that don’t match a CASE condition?
For this part I’m not 100% sure what MySQL will do, as I’m more of a sql server guy. I suspect, however, that your CASE statement results in NULL, which is then assigned to FieldName. It’s also possible that MySQL will decide not to change anything, but I would still expect it to report all the rows that match your WHERE clause as updated.
You should probably have an
ELSE FieldNameat the end of your case statement to be certain you get the latter behavior (no change) rather than the former (set to NULL).