MySQL提供了两种检查boolean列真值的方法,即column_variable = true和column_variable is true。我创建了一个表,插入了一些值并尝试了一些select语句。结果如下:
boolean
column_variable = true
column_variable is true
select
首先,我创建了此表:
mysql> create table bool_test ( -> id int unsigned not null auto_increment primary key, -> flag boolean ); Query OK, 0 rows affected (0.13 sec)
然后我插入了4行:
mysql> insert into bool_test(flag) values (true),(false),(9),(null); mysql> select * from bool_test; +----+------+ | id | flag | +----+------+ | 1 | 1 | | 2 | 0 | | 3 | 9 | | 4 | NULL |
这是select我在此表上触发的所有查询:
mysql> select * from bool_test where flag; +----+------+ | id | flag | +----+------+ | 1 | 1 | | 3 | 9 | +----+------+ 2 rows in set (0.49 sec) mysql> select * from bool_test where flag = true; +----+------+ | id | flag | +----+------+ | 1 | 1 | +----+------+ 1 row in set (0.02 sec) mysql> select * from bool_test where flag is true; +----+------+ | id | flag | +----+------+ | 1 | 1 | | 3 | 9 | +----+------+ 2 rows in set (0.04 sec) mysql> select * from bool_test where flag = false; +----+------+ | id | flag | +----+------+ | 2 | 0 | +----+------+ 1 row in set (0.01 sec) mysql> select * from bool_test where flag is false; +----+------+ | id | flag | +----+------+ | 2 | 0 | +----+------+ 1 row in set (0.00 sec) mysql> select * from bool_test where !flag; +----+------+ | id | flag | +----+------+ | 2 | 0 | +----+------+ 1 row in set (0.00 sec) mysql> select * from bool_test where not flag; +----+------+ | id | flag | +----+------+ | 2 | 0 | +----+------+ 1 row in set (0.00 sec) mysql> select * from bool_test where flag != true; +----+------+ | id | flag | +----+------+ | 2 | 0 | | 3 | 9 | +----+------+ 2 rows in set (0.00 sec) mysql> select * from bool_test where flag is not true; +----+------+ | id | flag | +----+------+ | 2 | 0 | | 4 | NULL | +----+------+ 2 rows in set (0.00 sec) mysql> select * from bool_test where flag != false; +----+------+ | id | flag | +----+------+ | 1 | 1 | | 3 | 9 | +----+------+ 2 rows in set (0.04 sec) mysql> select * from bool_test where flag is not false; +----+------+ | id | flag | +----+------+ | 1 | 1 | | 3 | 9 | | 4 | NULL | +----+------+ 3 rows in set (0.00 sec)
我的问题是:这是在建议使用is/ is not,当是明智的使用=/!= 与true/ false?哪一个是独立于供应商的?
is
is not
=
!=
true
false
MySQL实际上在欺骗您。它根本没有布尔列类型:
BOOL, BOOLEAN 这些类型是的同义词TINYINT(1)。零值被认为是错误的。非零值被认为是正确的:
BOOL, BOOLEAN
BOOL
BOOLEAN
这些类型是的同义词TINYINT(1)。零值被认为是错误的。非零值被认为是正确的:
TINYINT(1)
此外,布尔文字也不是这样的:
常数TRUE和分别FALSE取值为1和0。
TRUE
FALSE
考虑到:
0
1
我的结论是:
WHERE IS flag
WHERE flag
编辑: 如果必须跨平台,我会这样做:
WHERE flag=0 WHERE flag<>0
我敢肯定我们都做了很多次。