-3

I have a field visible. I want to change this value based on the visible current value, then update it to the new value.

E.g.

If visible == 0 change to 1

If visible == 1 change to 0

treyBake
  • 6,277
  • 6
  • 25
  • 54
Sławomir Kudła
  • 193
  • 2
  • 3
  • 13
  • 1
    Yes you can use either `IF()` function, or `CASE WHEN` statement(s). Refer [Control Flow Statements](https://dev.mysql.com/doc/refman/8.0/en/control-flow-functions.html) – Madhur Bhaiya Oct 04 '18 at 13:04
  • 2
    Possible duplicate of [MySQL update CASE WHEN/THEN/ELSE](https://stackoverflow.com/questions/12754470/mysql-update-case-when-then-else) – Lelio Faieta Oct 04 '18 at 13:04

2 Answers2

1

This way should work

UPDATE table SET visible = IF(visible = 0, 1, 0) ...
Ntwobike
  • 2,078
  • 1
  • 19
  • 26
1

Use case when

update table set visible=case when visible=0 then 1 when visible=1 then 0 end
Fahmi
  • 36,607
  • 5
  • 19
  • 28