2016-11-01 142 views
0

我想从我的表offer中选择数据,其中有一个字段名为type,我需要对每种类型应用不同的条件。SQL Server根据不同字段值的条件,选择不同的字段值

代码可能是这样的:

select * from offer where 
if type = 1 then apply condition_1, 
else if type = 2 then apply condition_2 and condition_3, 
else if type = 3 then apply condition_4, 

所以,我怎么能做到这一点?

+1

这将有助于如果你能发布什么样的条件.. –

回答

5
select * from offer 
where (type =1 and condition_1) 
or (type = 2 and condition_2 and condition_3) 
or (type = 3 and condition_3) 
0
select * from offer 
where CASE WHEN [type] = 1 then condition_1 
      WHEN [type] = 2 then (condition_2 and condition_3) 
      WHEN [type] = 3 then apply condition_4 END` 
相关问题