我們可以在 MySQL 結果集中使用一個字串來替換數字嗎?
是的,我們可以使用 CASE 語句來實現。我們首先建立一個表 -
mysql> create table DemoTable -> ( -> isMarried boolean -> ); Query OK, 0 rows affected (0.76 sec)
使用 insert 命令插入一些記錄到表格中 -
mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.19 sec)
使用 select 語句顯示錶中的所有記錄 -
mysql> select *from DemoTable;
輸出
+-----------+ | isMarried | +-----------+ | 1 | | 0 | | 0 | | 1 | +-----------+ 4 rows in set (0.00 sec)
以下是要用一個字串替換 MySQL 結果集中數字的查詢 -
mysql> select isMarried, CASE isMarried WHEN 1 THEN 'Married' ELSE 'Not Married' END AS Status from DemoTable -> ;
輸出
+-----------+-------------+ | isMarried | Status | +-----------+-------------+ | 1 | Married | | 0 | Not Married | | 0 | Not Married | | 1 | Married | +-----------+-------------+ 4 rows in set (0.00 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP