如何將bool轉換為MySQL中的int?\n


要將bool轉換為MySQL中的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)

更新於: 2019年7月30日

1K+瀏覽

啟動你的 職業

完成該課程以獲得認證

入門
廣告
© . All rights reserved.