将mysql boolean返回为“yes”或“no”

时间:2022-12-05 10:00:17

I have a BOOLEAN type column in my table. I would like to convert the 0/1 to Yes/No when the results are returned.

我的表中有一个BOOLEAN类型列。我想在返回结果时将0/1转换为是/否。

I found a response on this thread: Echo boolean field as yes/no or other values

我在这个线程上找到了一个响应:Echo布尔字段为yes / no或其他值

The response mentioned an IF THEN statement, but when I try, I only get a complaint from MySQL that there is a syntax error. Here is the line I am using:

响应中提到了IF THEN语句,但是当我尝试时,我只收到来自MySQL的抱怨语法错误。这是我正在使用的行:

IF qz.quiz_enabled == 1 THEN 'yes' ELSE 'no' AS enabled

Here is the error:

这是错误:

use near 'qz.quiz_enabled == 1 THEN 'yes' ELSE 'no' AS enabled

2 个解决方案

#1


21  

select case when qz.quiz_enabled 
            then 'yes' 
            else 'no' 
       end

or

要么

select if(qz.quiz_enabled, 'yes', 'no')

SQLFiddle demo

#2


0  

http://dev.mysql.com/doc/refman/5.0/en/if.html

http://dev.mysql.com/doc/refman/5.0/en/if.html

BEGIN
DECLARE s VARCHAR(20);

IF n > m THEN SET s = '>';
ELSEIF n = m THEN SET s = '=';
ELSE SET s = '<';
END IF;

SET s = CONCAT(n, ' ', s, ' ', m);

RETURN s;
END 

#1


21  

select case when qz.quiz_enabled 
            then 'yes' 
            else 'no' 
       end

or

要么

select if(qz.quiz_enabled, 'yes', 'no')

SQLFiddle demo

#2


0  

http://dev.mysql.com/doc/refman/5.0/en/if.html

http://dev.mysql.com/doc/refman/5.0/en/if.html

BEGIN
DECLARE s VARCHAR(20);

IF n > m THEN SET s = '>';
ELSEIF n = m THEN SET s = '=';
ELSE SET s = '<';
END IF;

SET s = CONCAT(n, ' ', s, ' ', m);

RETURN s;
END