-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,440
  • 6
  • 26
  • 57
Sławomir Kudła
  • 243
  • 2
  • 4
  • 14
  • 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,406
  • 1
  • 21
  • 27
1

Use case when

update table set visible=case when visible=0 then 1 when visible=1 then 0 end
Fahmi
  • 37,315
  • 5
  • 22
  • 31