在postgresql中翻转布尔值

时间:2015-08-16 05:37:18

标签: sql postgresql

我的表格中有Boolean类型字段(PostgreSQL 9.0)

有没有办法编写一个查询,将该字段更新为相反的值,而不知道当前有什么?

如果是True,则会更新为False

如果是False,则会更新为True

基本上我在问是否有一种机制允许Boolean类型与1 bit相同。例如,在C中如果x可以是{1,0},则可以只编写x=!x,如果是0则为1,如果是1,则为0它将是IF。不需要apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "burning.tutorial" minSdkVersion 21 targetSdkVersion 22 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } packagingOptions { exclude 'LICENSE.txt' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) // App's dependencies, including test compile 'com.android.support:support-annotations:22.2.0' // JMockit androidTestCompile 'org.jmockit:jmockit:1.18' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2' androidTestCompile 'com.android.support.test:runner:0.3' } 声明等......

1 个答案:

答案 0 :(得分:4)

为什么不使用operator NOT?

Update tableName
set c = NOT c 
where ...

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

SqlFiddle

中的示例