MYSQL update the only x'th number in integer/bit datatype field

时间:2015-08-06 13:54:13

标签: mysql sql-update

I created 9 columns each which hold either a 0 or a 1 integer. They exist so that users can toggle certain things on and off. I'd like to get rid of these 9 columns and instead use only one column where i can hold 9 0's and 1's for the 9 things my users will toggle. So say i have 000000000 and my user decides to toggle the first function, how would an update query for table accounts and column toggletimers look like which would change the value to 100000000 and so on for the 9 numbers?

1 个答案:

答案 0 :(得分:1)

  1. 您通常在数据库设计中将多个字段合并为一个字段(违反"1NF"
  2. 如果您真的想要,请使用bitwise operationsupdate accounts set toggletimers = toggletimers | (1 << (9-1)) where ...