MySQL 如何将bool转换为int

MySQL 如何将bool转换为int

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

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

以下是使用插入命令向表中插入一些记录的查询:

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中将布尔转换为整数的查询:

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)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程