2016-11-01 7 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` 
関連する問題