MySQL: Simple way to toggle a value of an int field

前端 未结 9 1616
忘掉有多难
忘掉有多难 2021-01-30 10:19

I know how to do this, but i think I\'ll overcomplicate it with double selects and so on.

How can you do this (example in pseudo-sql)

UPDATE some_table S         


        
相关标签:
9条回答
  • 2021-01-30 11:12

    Another option:

    UPDATE some_table SET an_int_value = ABS(an_int_value - 1);
    
    0 讨论(0)
  • 2021-01-30 11:17
    UPDATE some_table SET an_int_value = IF(an_int_value=1, 0, 1);
    

    http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_if

    0 讨论(0)
  • 2021-01-30 11:21

    For ENUM(0,1) fields you can use...

    UPDATE table SET int_value=BINARY(int_value=1)

    0 讨论(0)
提交回复
热议问题