2

I wanted to say something like:

 if field_1 then set field_2 = 1

Meaning, if field_1 holds a truthy value, do something else. Can an IF statement in sql execute this kind of evaluation?

Joseph Erickson
  • 938
  • 3
  • 8
  • 24

1 Answers1

2

Use CASE WHEN THEN and set field2 to 1 if the CASE WHEN is true, else use ELSE to set field2 to what it was before.

UPDATE table 
SET field2 = CASE
WHEN field1 IS NOT NULL 
THEN 1
ELSE field2
END;
baao
  • 71,625
  • 17
  • 143
  • 203
  • this is a really nice way to do this, and I was familiar with IS NOT NULL, so I guess you can't make a truthy evaluation? – Joseph Erickson Oct 10 '16 at 16:43
  • Everything but null is true in mysql @JosephErickson – baao Oct 10 '16 at 16:44
  • @JosephErickson if you wan't to check against the string `true` or `1`, simply replace `is not null` with `=1` or `='true'` – baao Oct 10 '16 at 16:45