Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL replace result value with another value

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 ?

like image 537
Amit Kumar Avatar asked Dec 20 '22 15:12

Amit Kumar


1 Answers

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 
like image 111
Kickstart Avatar answered Jan 04 '23 19:01

Kickstart