Mysql CASE and UPDATE

Posted by Rosengusta Garrett on Stack Overflow See other posts from Stack Overflow or by Rosengusta Garrett
Published on 2012-10-03T21:32:24Z Indexed on 2012/10/03 21:37 UTC
Read the original article Hit count: 338

Filed under:

I asked yesterday how I could update only the first column that was empty. I got this of a answer:

UPDATE `names` SET 
`name_1` = CASE WHEN `name_1` = '' then 'Jimmy' else `name_1` end,
`name_2` = CASE WHEN `name_1` != '' and `name_2` = '' then 'Jimmy' else `name_2` end

I tried it and it ended up updating every column with 'Jimmy' what's wrong with this? I can't find anything. It could possibly be the structure of the database. So here is what each name_* column is setup like:

#   Name    Type         Collation          Attributes     Null         Default Extra
 1  name_1  varchar(255) latin1_swedish_ci                 No            None   

© Stack Overflow or respective owner

Related posts about mysql