13.4 BOOLEAN

本教程将向您展示如何使用MySQL BOOLEAN数据类型来存储布尔值:truefalse

MySQL BOOLEAN数据类型简介

MySQL没有内置的布尔类型。 但是它使用TINYINT(1)。 为了更方便,MySQL提供BOOLEANBOOL作为TINYINT(1)的同义词。

在MySQL中,0被认为是false,非零值被认为是true。 要使用布尔文本,可以使用常量TRUEFALSE来分别计算为10。 请参阅以下示例:

SELECT true, false, TRUE, FALSE, True, False;
-- 1 0 1 0 1 0

执行上面代码,得到以下结果 -

mysql> SELECT true, false, TRUE, FALSE, True, False;
+------+-------+------+-------+------+-------+
| TRUE | FALSE | TRUE | FALSE | TRUE | FALSE |
+------+-------+------+-------+------+-------+
|    1 |     0 |    1 |     0 |    1 |     0 |
+------+-------+------+-------+------+-------+
1 row in set

MySQL BOOLEAN示例

MySQL将布尔值作为整数存储在表中。为了演示,让我们来看下面的tasts表:

USE testdb;

CREATE TABLE tasks (
    id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(255) NOT NULL,
    completed BOOLEAN
);

上面创建表语句中,即使将completed列指定为BOOLEAN类型,当显示表定义时,它是却是TINYINT(1)类型,如下所示:

DESCRIBE tasks;

以下语句向tasts表中插入2行数据:

INSERT INTO tasks(title,completed)
VALUES('Master MySQL Boolean type',true),
      ('Design database table',false);

在将数据保存到布尔列之前,MySQL将其转换为10,以下查询从tasks表中检索数据:

SELECT 
    id, title, completed
FROM
    tasks; 

+----+---------------------------+-----------+
| id | title                     | completed |
+----+---------------------------+-----------+
|  1 | Master MySQL Boolean type |         1 |
|  2 | Design database table     |         0 |
+----+---------------------------+-----------+
2 rows in set

如上所见, truefalse 分别被转换为10

因为Boolean类型是TINYINT(1)的同义词,所以可以在布尔列中插入10以外的值。如下示例:

INSERT INTO tasks(title,completed)
VALUES('Test Boolean with a number',2);

上面语句,工作正常~,查询tasts表中的数据,如下所示 -

mysql> SELECT 
    id, title, completed
FROM
    tasks; 
+----+----------------------------+-----------+
| id | title                      | completed |
+----+----------------------------+-----------+
|  1 | Master MySQL Boolean type  |         1 |
|  2 | Design database table      |         0 |
|  3 | Test Boolean with a number |         2 |
+----+----------------------------+-----------+
3 rows in set

如果要将结果输出为truefalse,可以使用IF函数,如下所示:

SELECT 
    id, 
    title, 
    IF(completed, 'true', 'false') completed
FROM
    tasks;

执行上面查询语句,得到结果如下所示 -

+----+----------------------------+-----------+
| id | title                      | completed |
+----+----------------------------+-----------+
|  1 | Master MySQL Boolean type  | true      |
|  2 | Design database table      | false     |
|  3 | Test Boolean with a number | true      |
+----+----------------------------+-----------+
3 rows in set

MySQL BOOLEAN运算符

要在tasts表中获取所有完成的任务,可以执行以下查询:

SELECT 
    id, title, completed
FROM
    tasks
WHERE
    completed = TRUE;

执行上面查询语句,得到结果如下所示 -

+----+---------------------------+-----------+
| id | title                     | completed |
+----+---------------------------+-----------+
|  1 | Master MySQL Boolean type |         1 |
+----+---------------------------+-----------+
1 row in set

如您所见,它只返回completed列的值为1的任务。要解决它,必须使用IS运算符:

SELECT 
    id, title, completed
FROM
    tasks
WHERE
    completed IS TRUE;

执行上面查询语句,得到结果如下所示 -

+----+----------------------------+-----------+
| id | title                      | completed |
+----+----------------------------+-----------+
|  1 | Master MySQL Boolean type  |         1 |
|  3 | Test Boolean with a number |         2 |
+----+----------------------------+-----------+
2 rows in set

在这个例子中,我们使用IS运算符来测试一个与布尔值的值。

要获得待处理(未完成)的任务,请使用IS FALSEIS NOT TRUE,如下所示:

SELECT 
    id, title, completed
FROM
    tasks
WHERE
    completed IS NOT TRUE;

执行上面查询语句,得到结果如下所示 -

+----+-----------------------+-----------+
| id | title                 | completed |
+----+-----------------------+-----------+
|  2 | Design database table |         0 |
+----+-----------------------+-----------+
1 row in set

在本教程中,您已经学习了如何使用MySQL BOOLEAN数据类型(它是TINYINT(1)的同义词),以及如何操作布尔值。

Last updated