1

例如,查询可以返回“是”或“否”。如果是,我怎样才能让它返回 1,如果不是,我怎样才能让它返回 0?

4

1 回答 1

3

如果您只想要 ayes如果它是 1,并且每个其他值都会导致 a 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
于 2012-04-17T16:41:14.927 回答