技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

如何在 MySQL 中将 bool 转换为 int?\

mysqlmysqli database

要在 MySQL 中将 bool 转换为 int,可以使用 CAST()。让我们首先创建一个表:

mysql> create table convertBoolToIntDemo
   -> (
   -> isYoung bool
   -> );
Query OK, 0 rows affected (0.69 sec)

下面是使用 insert 命令在表中插入一些记录的查询:

mysql> insert into convertBoolToIntDemo values(true);
Query OK, 1 row affected (0.18 sec)

mysql> insert into convertBoolToIntDemo values(false);
Query OK, 1 row affected (0.09 sec)

mysql> insert into convertBoolToIntDemo values(true);
Query OK, 1 row affected (0.15 sec)

mysql> insert into convertBoolToIntDemo values(false);
Query OK, 1 row affected (0.18 sec)

以下是使用 select 命令显示表中记录的查询:

mysql> select *from convertBoolToIntDemo;

这将产生以下输出 −

+---------+
| isYoung |
+---------+
|       1 |
|       0 |
|       1 |
|       0 |
+---------+
4 rows in set (0.00 sec)

以下是在 MySQL 中将 bool 转换为 int 的查询:

mysql> select cast(isYoung=1 AS SIGNED INTEGER) from convertBoolToIntDemo;

这将产生以下输出 −

+-----------------------------------+
| cast(isYoung=1 AS SIGNED INTEGER) |
+-----------------------------------+
|                                 1 |
|                                 0 |
|                                 1 |
|                                 0 |
+-----------------------------------+
4 rows in set (0.00 sec)

相关文章