您可以为此使用select case语句。语法如下。
select yourColumnName1,yourColumnName2,...N, case when yourColumnName=1 then 'true' else 'false' end as anyVariableName from yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下。
mysql> create table selectReturnDemo -> ( -> Id int, -> Name varchar(100), -> isGreaterthan18 tinyint(1) -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下。
mysql> insert into selectReturnDemo values(1,'Carol',0); mysql> insert into selectReturnDemo values(2,'Bob',1); mysql> insert into selectReturnDemo values(3,'Mike',1); mysql> insert into selectReturnDemo values(4,'David',0); mysql> insert into selectReturnDemo values(5,'Adam',1);
使用select语句显示表中的所有记录。查询如下。
mysql> select *from selectReturnDemo;
以下是输出。
+------+-------+-----------------+ | Id | Name | isGreaterthan18 | +------+-------+-----------------+ | 1 | Carol | 0 | | 2 | Bob | 1 | | 3 | Mike | 1 | | 4 | David | 0 | | 5 | Adam | 1 | +------+-------+-----------------+ 5 rows in set (0.00 sec)
这是用select return替换值的查询。查询如下。
mysql> select Id,Name, -> case when isGreaterthan18=1 then 'true' -> else 'false' -> end as AgeIsGreaterthan18 -> from selectReturnDemo;
以下是输出。
+------+-------+--------------------+ | Id | Name | AgeIsGreaterthan18 | +------+-------+--------------------+ | 1 | Carol | false | | 2 | Bob | true | | 3 | Mike | true | | 4 | David | false | | 5 | Adam | true | +------+-------+--------------------+ 5 rows in set (0.00 sec)