0

只是一个快速的问题要问

在 MySQL 中,我如何将数值显示为字符串等价物?

所以

1 = New
2 = Old
3 = Cancelled

所以如果我有类似的东西

SELECT object_name, object_status where object_status > 0

这又回来了

object#1, 1
object#2, 2
object#3, 1
object#4, 3

但我希望它显示

object#1, New
object#2, Old
object#3, New
object#4, Cancelled

提前感谢KS

4

3 回答 3

4

你可以用CASE这个,

SELECT object_name,         
        CASE object_status 
            WHEN 1 THEN 'New'
            WHEN 2 THEN 'Old'
            WHEN 3 THEN 'Cancelled'
            ELSE NULL
        END status
where object_status > 0

或者

SELECT object_name,         
        CASE 
            WHEN object_status = 1 THEN 'New'
            WHEN object_status = 2 THEN 'Old'
            WHEN object_status = 3 THEN 'Cancelled'
            ELSE NULL
        END status
where object_status > 0
于 2013-02-08T15:38:01.147 回答
2
SELECT object_name, 
 case object_status when 1 then 'New' 
  when 2 then 'Old' 
  when 3 then 'Cancelled' end as status
from myTable
where object_status > 0
于 2013-02-08T15:38:43.640 回答
2

您可以创建一个新的映射表并将这两个表连接在一起。新表将有 2 列 - 一列用于object_status,一列用于好名称。然后加入两者。

SELECT s.object_name, sm.StatusName
FROM Status s
INNER JOIN StatusMapping sm on s.object_status = sm.object_status
WHERE s.object_status > 0

这将允许您在不更改查询的情况下根据需要添加新映射。

于 2013-02-08T15:41:13.760 回答