SQL更改逐个结果

时间:2012-07-03 18:29:05

标签: mysql sql-server sqlite sql

所以我有这个代码它工作正常

SELECT NAME, 
       Country, 
       NORMAL_STATE, 
       CURRENT_STATE
FROM SOMS_TABLE
WHERE SOMS_TABLE.NAME LIKE 'AR%' AND 
      NORMAL_STATE <> CURRENT_STATE
ORDER BY SOMS_TABLE.Country ASC

输出为
enter image description here

但我希望将1和0更改为OPEN&amp;已结束

enter image description here
谢谢你

3 个答案:

答案 0 :(得分:3)

select name,
    Country,
    case NORMAL_STATE
        when 1 then 'OPEN'
        when 0 then 'CLOSED'
        else 'UNKNOWN'
    end as NORMAL_STATE,
    case CURRENT_STATE
        when 1 then 'OPEN'
        when 0 then 'CLOSED'
        else 'UNKNOWN'
    end as CURRENT_STATE
from SOMS_TABLE
where name like 'AR%'
    and NORMAL_STATE <> CURRENT_STATE
order by Country 

答案 1 :(得分:1)

使用此

CASE NORMAL_STATE
    WHEN 1 THEN 'OPEN'
    WHEN 2 THEN 'CLOSED'
END AS NORMAL_STATE

同样适用于CURRENT_STATE

答案 2 :(得分:0)

您可以使用IF语句

SELECT NAME, Country, 
       IF(NORMAL_STATE > 0,'OPEN','CLOSED'), 
       IF(CURRENT_STATE > 0,'OPEN','CLOSED') 
FROM SOMS_TABLE 
WHERE SOMS_TABLE.NAME LIKE 'AR%' AND 
      NORMAL_STATE <> CURRENT_STATE 
ORDER BY SOMS_TABLE.Country ASC