Search code examples
mysqlsqlreplacemappingcase

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.


Solution

  • You would use a case statement, such as this:

    select (case when status = 0 then 'disabled'
                 when status = 1 then 'enabled'
            end)
    from . . .