How do I combine two UPDATE statements in one MySQL query?

后端 未结 6 1326
野的像风
野的像风 2020-12-18 11:44

Greetings,

How would one go about performing two UPDATE statements in one query, for example:

UPDATE albums SET isFeatured = \'0\' WHERE isFeatured          


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 11:47

    You can use CASE WHEN statement and remember to set original value where necessary (ELSE clause below) and order CASE conditions as required (in statement below isFeatured will be 0 if row having requested id also has isFeatured = 1, to change it swap WHEN clauses).

    UPDATE albums 
    SET isFeatured = CASE 
      WHEN isFeatured = '1' THEN '0' 
      WHEN id = '$id' THEN '1'
      ELSE isFeatured
    END
    

提交回复
热议问题