Ivo San
Ivo San

Reputation: 1253

Proper way of conditionting with mysql to update different column with different conditions

column1 = value     WHERE cond='1'
column2 = value     WHERE cond='2'

In the example above. Is this the right way to code it in mysql

UPDATE table SET (column1='value' WHERE cond='1') OR (column2='value' WHERE cond='2')

Upvotes: 0

Views: 18

Answers (1)

Aguardientico
Aguardientico

Reputation: 7779

UPDATE
   table
SET
   column1 = (CASE cond WHEN '1' THEN 'value' ELSE column1 END)
 , column2 = (CASE cond WHEN '2' THEN 'value' ELSE column2 END)
WHERE
  cond IN ('1', '2')
;

Upvotes: 1

Related Questions