MySQL Tutorial/Data Types/Boolean

Материал из SQL эксперт
Перейти к: навигация, поиск

BOOL, BOOLEAN

These types are synonyms for TINYINT(1).

A value of zero is considered false.

Non-zero values are considered true:

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



mysql>
mysql> SELECT TRUE, true, FALSE, false;
+------+------+-------+-------+
| TRUE | TRUE | FALSE | FALSE |
+------+------+-------+-------+
|    1 |    1 |     0 |     0 |
+------+------+-------+-------+
1 row in set (0.00 sec)
mysql>


The values TRUE and FALSE are merely aliases for 1 and 0, respectively, as shown here:

mysql>
mysql> SELECT IF(0 = FALSE, "true", "false");
+--------------------------------+
| IF(0 = FALSE, "true", "false") |
+--------------------------------+
| true                           |
+--------------------------------+
1 row in set (0.00 sec)
mysql> SELECT IF(1 = TRUE, "true", "false");
+-------------------------------+
| IF(1 = TRUE, "true", "false") |
+-------------------------------+
| true                          |
+-------------------------------+
1 row in set (0.00 sec)
mysql> SELECT IF(2 = TRUE, "true", "false");
+-------------------------------+
| IF(2 = TRUE, "true", "false") |
+-------------------------------+
| false                         |
+-------------------------------+
1 row in set (0.00 sec)
mysql> SELECT IF(2 = FALSE, "true", "false");
+--------------------------------+
| IF(2 = FALSE, "true", "false") |
+--------------------------------+
| false                          |
+--------------------------------+
1 row in set (0.00 sec)
mysql>


Using if statement with BOOLEAN

mysql>
mysql> SELECT IF(0, "true", "false");
+------------------------+
| IF(0, "true", "false") |
+------------------------+
| false                  |
+------------------------+
1 row in set (0.00 sec)
mysql> SELECT IF(1, "true", "false");
+------------------------+
| IF(1, "true", "false") |
+------------------------+
| true                   |
+------------------------+
1 row in set (0.00 sec)
mysql> SELECT IF(2, "true", "false");
+------------------------+
| IF(2, "true", "false") |
+------------------------+
| true                   |
+------------------------+
1 row in set (0.00 sec)
mysql>
mysql>
mysql>