如何为SELECT返回值赋值?

时间:2012-04-17 16:40:38

标签: sql

例如,查询可以返回“是”或“否”。如果它是肯定的话我怎么能让它返回1而如果它是否则它返回0?

1 个答案:

答案 0 :(得分:3)

如果您只需要yes,如果它是1,而其他每个值都会产生no,则可以执行以下操作:

select case 
        when MyColumn = 'yes' then 1 
        else 0 
    end as MyDerivedValue
from MyTable

或者,您可以更具体:

select case MyColumn 
        when 'yes' then 1 
        when 'no' then 0
        when 'maybe' then 2
        else -1
    end as MyDerivedValue
from MyTable