Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map keys to values for an individual field in a MySQL select query

Supposing we have a field status (0 for disabled, 1 for enabled), and we need to get the literal values directly using MySQL, in other words: if we requested the query:

select status 
from `<table>`

We need the column to appear like this:

  status
----------
 disabled
 enabled

not as following:

 status
--------
   0
   1

Knowing that we don't have a mysql status table to join with and get the values as usual.

like image 221
Yazid Erman Avatar asked Apr 22 '13 13:04

Yazid Erman


1 Answers

You would use a case statement, such as this:

select (case when status = 0 then 'disabled'
             when status = 1 then 'enabled'
        end)
from . . .
like image 195
Gordon Linoff Avatar answered Oct 23 '22 17:10

Gordon Linoff