开发者

MySQL boolean - flipping the value?

开发者 https://www.devze.com 2023-01-12 04:44 出处:网络
MySQL uses TinyINT to serve as a boolean field. Given the possible options of 0 and 1, I decided that I\'d flip values like this:

MySQL uses TinyINT to serve as a boolean field. Given the possible options of 0 and 1, I decided that I'd flip values like this:

UPDATE table
SET boolean_field = ABS(boolean_field - 1)
WHERE Circle-K = 'Strange things are afoot'

So you either go 1 -> 0 -> ABS(0) = 0

or 0 -> -1 -> ABS(-1) = 1

now I'm curious if this is 开发者_如何学Cacceptable or horrifying to the real programmers?

/me is a beginner


Why not simply use:

UPDATE the_table
   SET boolean_field = NOT boolean_field
WHERE ...

Makes your intention a lot easier to read


You can also use field = 1 - field or field = ! field

0

精彩评论

暂无评论...
验证码 换一张
取 消