如何將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)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP