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
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 FALSE或IS 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