Claim your Biolink Click Here
0 like 0 dislike
264 views
How can I use an if/else condition on a select statement in mysql?
in Education & Reference by (820 points) | 264 views

1 Answer

0 like 0 dislike

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.
by
edited

Related questions

0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
2 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
1 answer
2 like 0 dislike
1 answer
0 like 0 dislike
1 answer

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,310 questions
1,471 answers
569 comments
4,809 users