简体   繁体   English

在PostgreSQL中翻转布尔值

[英]flipping boolean value in postgresql

I have a Boolean type field in my table (PostgreSQL 9.0) 我的表格中有一个Boolean类型字段(PostgreSQL 9.0)

Is there a way to write a query that update this field to the opposite value without knowing what is currently there? 有没有一种方法可以编写一个查询来将该字段更新为相反的值,而又不知道当前存在什么?

if it's True then it will be updated to False . 如果为True ,则将其更新为False

if it's False then it will be updated to True . 如果为False则它将更新为True

Basically I'm asking if there is a mechanism that allows treating Boolean type same as 1 bit . 基本上,我在问是否有一种机制可以将Boolean类型与1 bit相同。 For example in C if x can be {1,0} you can simply write x=!x and if it was 0 it will be 1 , if it was 1 it will be 0 . 例如,在C如果x可以是{1,0}你可以简单的写x=!x ,如果是0这将是1 ,如果是1这将是0 No need for IF statment etc... 无需IF声明等...

Why not to use operator NOT? 为什么不使用运算符NOT?

Update tableName
set c = NOT c 
where ...

http://www.postgresql.org/docs/current/static/functions-logical.html http://www.postgresql.org/docs/current/static/functions-logical.html

Example in SqlFiddle SqlFiddle中的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM