MySQL replace result value with another value

Amit Kumar picture Amit Kumar · Oct 12, 2012 · Viewed 10.3k times · Source

I have MySQL and the query is:

select name,re_type from myTable

I want to replace all values of type:

1 = student
2 = teacher 

So the result should be:

name    re_type
---------------
Ron     Student
Mac     Teacher

Not like:

name    re_type
---------------
Ron     1
Mac     2

Is it possible to make a query like that so I get the desired result in MySQL ?

Answer

Kickstart picture Kickstart · Oct 12, 2012

You can use a CASE statement

SELECT name, CASE WHEN re_type = 1 THEN 'Student' WHEN re_type = 2 THEN 'Teacher' ELSE 'Donno' END AS re_type_text
FROM myTable