2016-11-01 144 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` 
相關問題