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?
To deal with Boolean in MySQL, you can use BOOL or BOOLEAN or TINYINT(1). If you use BOOL or BOOLEAN, then MySQL internally converts it into TINYINT(1). In BOOL or BOOLEAN data type, if you use true literal then MySQL represents it as 1 and false literal as 0 like in PHP/ C/ C++ language.
You can update boolean value using UPDATE command. If you use the BOOLEAN data type, MySQL internally convert it into tinyint(1). It can takes true or false literal in which true indicates 1 to tinyint(1) and false indicates 0 to tinyint(1).
MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.
MySQL does not contain built-in Boolean or Bool data type. They provide a TINYINT data type instead of Boolean or Bool data types. MySQL considered value zero as false and non-zero value as true. If you want to use Boolean literals, use true or false that always evaluates to 0 and 1 value.
MySQL is actually fooling you. It doesn't have a boolean column type at all:
BOOL
,BOOLEAN
These types are synonyms for
TINYINT(1)
. A value of zero is considered false. Nonzero values are considered true:
Also, the boolean literals are not such:
The constants
TRUE
andFALSE
evaluate to 1 and 0, respectively.
Considering that:
0
or 1
in BOOLEAN
My conclusion would be:
WHERE IS flag
or just WHERE flag
because =
simply doesn't work correctly. Which one, is possibly a matter of preference.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With