我们可以在 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)
广告