[英]SQL: INSERT 1 if value=0 and 0 if value=1
I have data from an SQL query that is 'fileUnavailable' and need to insert into another table that is 'fileAvailable.' 我有一个来自SQL查询的数据为“ fileUnavailable”,并且需要插入到另一个名为“ fileAvailable”的表中。 The values are either 1 or 0 and I need to do an SQL insert.
值是1或0,我需要执行SQL插入。 Since fileUnavaiable and fileAvailable are opposite, I'd need to change the first value to either 1 if it's 0 or 0 if it's 1. Originally, I was thinking to just do an if, else statement to change the value, but that seems bulky and seems too simple for there not to be a method already.
由于fileUnavaiable和fileAvailable是相反的,因此我需要将第一个值更改为1(如果为0则为0)或0(如果为1)。本来,我原本只是想做一个if,else语句来更改该值,但这看起来笨重似乎太简单了,以至于还没有一种方法。
I'm mostly curious as to whether or not SQL has something like !(fileUnavailable) but works for 1 and 0 because these values are ints in my db. 我对SQL是否具有!(fileUnavailable)之类的东西非常好奇,但它适用于1和0,因为这些值在我的数据库中是整数。
Pseudo: 伪:
INSERT INTO table (fileAvailable) VALUES ( NOT($fileUnavailable));
Use a hint with ABS
: 对
ABS
使用提示:
UPDATE table SET field = ABS(field - 1)
So if field
is 1
, then field - 1
is 0
, abs(0)
is still 0
. 因此,如果
field
为1
,则field - 1
为0
, abs(0)
仍为0
。
And if field
is 0
, then field - 1
is -1
, abs(-1)
is 1
. 如果
field
为0
,则field - 1
为-1
, abs(-1)
为1
。
For query you provided in a question: 对于您在问题中提供的查询:
INSERT INTO table (fileAvailable) VALUES ( ABS($fileUnavailable - 1) );
I found this from another stack overflow question. 我是从另一个堆栈溢出问题中发现的。
INSERT INTO `table` SET `my_bool` = NOT my_bool
Update a boolean to its opposite in SQL without using a SELECT 在不使用SELECT的情况下将布尔值更新为相反的SQL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.