T-Sql如何按案例过滤

时间:2011-08-09 22:07:23

标签: tsql

如何根据具体情况进行过滤?

select * from address
where streetnum > 1000 only when state = MI otherwise select everything else

select * from address
where case when state = MI then streetnum > 1000

2 个答案:

答案 0 :(得分:2)

address获取所有行,但当state'MI'时,streetnum必须大于1000

select * from address
where streetnum > 1000 or state <> 'MI'

如果您想使用case,可能需要检查多个值。

select *
from address
where case state
       when 'MI' then 1000
       when 'MA' then 1000
      else 0
      end < streetnum

如果所有州都有相同的值(1000),那就与此相同。

select * 
from address
where streetnum > 1000 or state not in ('MI', 'MA')

答案 1 :(得分:2)

如果我正确地解释了你的问题,那么......

select * from address 
where state <> 'MI' or (state = 'MI' and streetnum > 1000)