mysql is true_MySQL:“ = true”与“是真的”在BOOLEAN上。何时建议使用哪个?哪个独立于供应商?...

bd96500e110b49cbb3cd949968f18be7.png

MySQL provides 2 ways to check truth value of boolean columns, those are column_variable = true and column_variable is true. I created a table, inserted few values & tried a few select statements. Here are the results:

First I created this table:

mysql> create table bool_test (

-> id int unsigned not null auto_increment primary key,

-> flag boolean );

Query OK, 0 rows affected (0.13 sec)

Then I inserted 4 rows:

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 |

Here are all the select queries I fired on this table:

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)

My Question is: when is it advisable to use is/is not and when is it advisable to use =/!= with true/false ? Which one is vendor independent?

解决方案BOOL, BOOLEAN

These types are synonyms for TINYINT(1). A value of zero is considered

false. Nonzero values are considered true:

The constants TRUE and FALSE evaluate to 1 and 0, respectively.

Considering that:

Many database systems do not have booleans either (not at least in standard SQL and column types)

MySQL doesn't have an easy way to enforce 0 or 1 in BOOLEAN

My conclusion would be:

You'll have to use WHERE IS flag or just WHERE flag because = simply doesn't work correctly. Which one, is possibly a matter of preference.

Whatever you choose, no option will be vendor independent. For instance, Oracle won't even run either of them.

Edit: if cross-platform is a must, I'd go for this:

WHERE flag=0

WHERE flag<>0

I'm sure we've all done it lots of times.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值