是的,我们可以使用CASE语句来做到这一点。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> isMarried boolean -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(true); mysql> insert into DemoTable values(false); mysql> insert into DemoTable values(false); mysql> insert into DemoTable values(true);
使用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)