12

假设我们有一个字段状态(0对于禁用,1对于启用),我们需要直接使用 MySQL 获取文字值,换句话说:如果我们请求查询:

select status 
from `<table>`

我们需要该列显示如下:

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

不如下:

 status
--------
   0
   1

知道我们没有 mysql 状态表可以像往常一样加入并获取值。

4

2 回答 2

17

您将使用 case 语句,例如:

select (case when status = 0 then 'disabled'
             when status = 1 then 'enabled'
        end)
from . . .
于 2013-04-22T13:25:03.070 回答
1

您还可以使用if控制流功能(因为您的status字段只能采用两个值 - [0,1]):

select if(status = 0, 'disabled', 'enabled')
from <...>
于 2015-04-28T00:09:27.337 回答