Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return mysql boolean as 'yes' or 'no'

Tags:

mysql

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.

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

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 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
like image 674
Lee Loftiss Avatar asked Jun 05 '13 21:06

Lee Loftiss


People also ask

Can MySQL return boolean?

MySQL doesn't really have booleans. TRUE and FALSE are aliases to 1 and 0, and the BOOL column type is just an alias for TINYINT(1) . All expressions that appear to give boolean results actually return 0 or 1.

How do you set a boolean to true in MySQL?

You can update boolean value using UPDATE command. If you use the BOOLEAN data type, MySQL internally convert it into tinyint(1). It can takes true or false literal in which true indicates 1 to tinyint(1) and false indicates 0 to tinyint(1).

Is 0 true or false in MySQL?

MySQL defines a value of 0 as false and a non-zero value as true. Therefore, to use Boolean literal values, you use the constants TRUE and FALSE that evaluate the value of 0 and 1. From the above, we can deduce that MySQL considers 1 and 0 as True and False, respectively.

Is there a Boolean data type in MySQL?

MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.


1 Answers

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

or

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

SQLFiddle demo

like image 114
juergen d Avatar answered Oct 27 '22 00:10

juergen d