Home > Database > Mysql Tutorial > body text

一个有趣的SQL命题 用一条语句切换BIT型的真假值

WBOY
Release: 2016-06-07 17:58:47
Original
1116 people have browsed it

有个需求,用一条SQL语句(SQL SERVER)的,切换某个BIT字段值。也就是说从TRUE变FALSE,从FALSE到TRUE。

当然,第一反应是用存储过程。判断原来这个字段值,然后UPDATE。
网上粗粗找了一下没找到方案。自己一动手,居然有个很有趣的结果,连WHERE都不用
对数据库的BIT型赋值,用SQL语句来操作和用管理界面输入不同。管理界面输入必须用TRUE或FALSE来填写。而SQL语句则用0或1
于是乎,先尝试:
代码如下:
updat t set b=b+1 where id=1;
select b from t where id=1;

b字段一直保持1
那么用减号呢
代码如下:
update t set b=b-1 where id=1;
select b from t where id=1;

可以发现每次结果就在0和1之间切换,SQL SERVER把-1当做1来处理。
这个需求很容易就解决了。
通常这在一些状态切换(或开关)的地方会用到,在需求中不需要知道切换后的情况。很有趣也很简单,呵呵
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!