MySQL中是否有一种方法可以通过一个查询来反转布尔字段? [英] Is there a way in MySQL to reverse a boolean field with one query?

查看:131
本文介绍了MySQL中是否有一种方法可以通过一个查询来反转布尔字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格中有一个标题为授权"的列.它的默认值是0.授权用户时需要将其更改为1,但是必须能够将其重置为0.我知道我可以通过2个这样的查询轻松地做到这一点:

I have a column in my table titled 'authorised'. Its default is 0. It needs to be changed to 1 when the user is authorised, but it must be able to be reset to 0. I know I could do this easily with 2 queries like so:

$authorised = Db::query('SELECT authorised FROM users WHERE id=2');

$newAuthValue = ($authorised['authorised']) ? 0 : 1;

Db::query('UPDATE users SET authorised=' . $newAuthValue . ' WHERE id=2');

我想知道的是,有一种方法可以通过一个查询来做到这一点吗?要反转布尔值?

What I wanted to know, is there a way to do this with one query? To reverse a boolean value?

推荐答案

UPDATE users SET `authorised` = IF (`authorised`, 0, 1)

这篇关于MySQL中是否有一种方法可以通过一个查询来反转布尔字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆