in operator inside if condition and where clause in sql 服务器
in operator inside if condition and where clause in sql server
我必须根据 where 条件获取相同的架构但不同的数据。以下是我的查询。我正在尝试在 where 条件中应用 CASE 语句。主要问题是在这里处理 in 运算符。我不想在整个块上应用 IF 条件,因为我原来的 select 语句非常大。如果除了使用 CASE 语句
还有其他方法,我没问题
--@input is an outside variable
select col1, col2, col3, col4, col5
from table
where col6 in
case where @input = 'web' then (18, 19, 20, 22)
case where @input = 'net' then (37, 69, 26, 25)
case where @input = 'com' then (55, 66, 46, 27)
else (55, 46, 46, 17)
end
只需使用正则布尔表达式:
where (@input = 'web' and col6 in (18, 19, 20, 22)) or
(@input = 'net' and col6 in (37, 69, 26, 25)) or
(@input = 'com' and col6 in (55, 66, 46, 27)) or
(@input not in ('web', 'net', 'com') and col6 in (55, 46, 46, 17))
我必须根据 where 条件获取相同的架构但不同的数据。以下是我的查询。我正在尝试在 where 条件中应用 CASE 语句。主要问题是在这里处理 in 运算符。我不想在整个块上应用 IF 条件,因为我原来的 select 语句非常大。如果除了使用 CASE 语句
还有其他方法,我没问题 --@input is an outside variable
select col1, col2, col3, col4, col5
from table
where col6 in
case where @input = 'web' then (18, 19, 20, 22)
case where @input = 'net' then (37, 69, 26, 25)
case where @input = 'com' then (55, 66, 46, 27)
else (55, 46, 46, 17)
end
只需使用正则布尔表达式:
where (@input = 'web' and col6 in (18, 19, 20, 22)) or
(@input = 'net' and col6 in (37, 69, 26, 25)) or
(@input = 'com' and col6 in (55, 66, 46, 27)) or
(@input not in ('web', 'net', 'com') and col6 in (55, 46, 46, 17))