MySQL CASE to update multiple columns

后端 未结 3 1066
走了就别回头了
走了就别回头了 2020-12-14 16:17

I would like to update multiple columns in my table using a case statement, but I cannot find how to do this (is this even possible). I came up with the following invalid re

3条回答
  •  萌比男神i
    2020-12-14 17:00

    UPDATE tablename
    SET col1 = CASE WHEN name = 'name1' THEN 5 
                    WHEN name = 'name2' THEN 3 
                    ELSE 0 
               END
     , col2 = CASE WHEN name = 'name1' THEN '' 
                   WHEN name = 'name2' THEN 'whatever' 
                   ELSE '' 
              END
    ;
    

提交回复
热议问题