格式化SQL输出,其中值为0,1或2

时间:2014-07-31 17:37:45

标签: sql

我正在使用包含以下列的数据库模式

TRAFFIC_DIRECTION 
tinyint 
The direction of traffic. Enum ( unknown = 0; inbound = 1; outbound = 2) 

当我运行查询时,TRAFFIC_DIRECTION显示为

TRAFFIC_DIRECTION
1
2
2
1
1
1
2
2
2
1
1
1
1
1

我如何做到这样,输出"入站"而不是2,输出"出站"

3 个答案:

答案 0 :(得分:2)

某些RDBMS支持ENUM类型,但它们都支持CASE语句。

SELECT id,
    CASE TRAFFIC_DIRECTION
        WHEN 0 THEN 'Unknown'
        WHEN 1 THEN 'Inbound'
        WHEN 2 THEN 'Outbound'
        ELSE 'ERROR'
    END
FROM Table1

答案 1 :(得分:1)

select case
         when traffic_direction = 1 then 'inbound'
         when traffic_direction = 2 then 'outbound'
         else 'unknown'
       end as direction
from the_table;

答案 2 :(得分:1)

SELECT
CASE WHEN TRAFFIC_DIRECTION = 0 THEN "UNKNOWN"
     WHEN TRAFFIC_DIRECTION = 1 THEN "inbound"
     WHEN TRAFFIC_DIRECTION = 2 THEN "outbound"
     END AS Traffic_Word
FROM sometable
WHERE some criteria