简体   繁体   English

在MySQL / Oracle数据库中切换布尔值

[英]Toggle boolean in MySQL / Oracle Database

Update myTable SET field = 1 (if field = 0 or if field is null) where myid = 12345;
Update myTable SET field = 0 (if field = 1) where myid = 12345;

What is the best way to transform this Pseudocode in proper SQL for Oracle and MySQL? 在适用于Oracle和MySQL的适当SQL中转换此伪代码的最佳方法是什么?

您可以像这样简单地使用模:

UPDATE myTable SET field = (field + 1) % 2 WHERE myId = 12345;

Due to the lack of a real boolean in both DBMS you need a case statement: 由于两个DBMS中都没有真正的布尔值,因此需要一个case语句:

update myTable
   set the_column = case when the_column = 1 then 0 else 1 end
where myId = 12345;

This assumes that the column never has different values than 0 and 1 这假设列的值永远不会与0和1不同

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

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