Saleen.S
Saleen.S

Reputation: 11

How to manipulate SQL Query output

I would like to know if it's possible for example I have one sql table with the following results

+----+---------+--------+--------+
| ID |  Name   | Number | Active |
+----+---------+--------+--------+
|  1 | Jessica |     12 |      0 |
|  2 | Andrew  |     23 |      1 |
|  3 | Jason   |     53 |      0 |
+----+---------+--------+--------+

And I would like to change the active field to 0 = No | 1 = Yes but only in the results, I don't want to change the value of the row, is it possible to make one query that can do it?

Well with the answers bellow I managed to get it changed but now how can I echo the value in php?

SELECT *, case when Active =0 then 'No' when Active =1 then 'Yes' end as Expr1,
FROM table

Should it be like: $isActive = $rows['Expr1']; NVM the line above is working.

Upvotes: 0

Views: 1984

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

A particularly simple way would use elt():

select Id, name, number,
       elt(Active + 1, 'No', 'Yes') as as active_status
from t

Upvotes: 0

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31993

use case when

select Id,name,number,
case  Active when 0 then 'No'
    when 1 then 'Yes' end as active_status
    from t

Upvotes: 0

George Joseph
George Joseph

Reputation: 5922

Just use a case statement for translating 1 = yes and 0 = No like this

select ID 
      ,Name
      ,Number
      ,case when Active=0 then 'No'
            when Active=1 then 'Yes'
       end as active_y_n
 from table

Upvotes: 3

Related Questions