I have not used the CASE statement much in MySQL and most of the examples on the web are pretty straight forward but there were none that showed more complex usages of the CASE statement.
In the following example I am checking a Q values and setting their values in the returned record set based
You can try this
SELECT username,
case
when(Q1_Value=1) then "Excellent"
WHEN(Q1_Value=2) then "Good"
WHEN(Q1_Value=3) then "Poor"
WHEN(Q1_Value=0) then "Null"
else Q1_Value end as Q1_Value,
case
WHEN(Q2_value=1) then "Good"
WHEN(Q2_value=2) then "Poor"
WHEN(Q2_value=0) then "Null"
else Q2_value end as Q2_Value,
case
when(Q3_Value=1) then "Excellent"
WHEN(Q3_Value=2) then "Good"
WHEN(Q3_Value=3) then "Poor"
WHEN(Q3_Value=0) then "Null"
else Q3_Value end as Q3_Value
FROM Table_Name
The great thing about this is you can return data based on what is in the database. Some people suggested doing this in the PHP code (I’m working with PHP just now) but I wanted to do it in the MySQL and keep it all in one place.