SQL 服务器 Select 不同字段值的 where 条件不同
SQL Server Select with different where conditions for different field values
我想从我的 table offer
中获取 select 数据,其中有一个名为 type
的字段,我需要对每种类型应用不同的条件。
代码可能如下所示:
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,
那么我该如何实现呢?
select * from offer
where (type =1 and condition_1 )
or ( type = 2 and condition_2 and condition_3 )
or (type = 3 and condition_3 )
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`
我想从我的 table offer
中获取 select 数据,其中有一个名为 type
的字段,我需要对每种类型应用不同的条件。
代码可能如下所示:
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,
那么我该如何实现呢?
select * from offer
where (type =1 and condition_1 )
or ( type = 2 and condition_2 and condition_3 )
or (type = 3 and condition_3 )
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`