多个与SQL Server中的where子句一样

时间:2017-06-29 05:41:12

标签: sql

我想用多个like case语句构建一个sql查询。这是我的SQL查询。 Case语句用于确保,这是来自Web站点还是Mobile的请求,如果请求来自网站那么case语句将工作其他明智的不需要。

以下是喜欢:

i.ItemNumber like '%-NLA' or 
i.ItemNumber like '%-NLA DEL' or 
i.ItemNumber like '%-NLAmod'

请帮帮我。

select count(p.SkuID) as Total from Product p (nolock)
join items i (nolock) on p.SkuID = i.SkuID
join #TempProductLine pl (nolock) on i.ProductLineID = pl.ProductLineID where p.ProductID = @ParentId and i.IsSelling = 1 and
case when @IsWebSite = 1 then
    PATINDEX ('%-NLA DEL',i.ItemNumber) 
end = 0

2 个答案:

答案 0 :(得分:0)

也许使用SIMILAR TO会有用吗?

SELECT * from table WHERE column SIMILAR TO '(AAA|BBB|CCC)%';

所以将它与i.ItemNumber SIMILAR TO '%(-NLA|-NLA DEL|-NLAmod)';

结合起来

您可能想尝试的其他一些事情:

LIKE ANY(ARRAY['%-NLA','%-NLA DEL','%-NLAmod'])

答案 1 :(得分:0)

试试这个:

select count(p.SkuID) as Total from Product p (nolock)
join items i (nolock) on p.SkuID = i.SkuID
join #TempProductLine pl (nolock) on i.ProductLineID = pl.ProductLineID   where 
p.ProductID = @ParentId and i.IsSelling = 1 and
i.ItemNumber like '%' + 
case 
when @IsWebSite = 1 then '-NLA'
when @IsWebSite = 2 then '-NLA DEL'
when @IsWebSite = 3 then '-NLAmod'
else '' end

或者只是这样:

select count(p.SkuID) as Total from Product p (nolock)
join items i (nolock) on p.SkuID = i.SkuID
join #TempProductLine pl (nolock) on i.ProductLineID = pl.ProductLineID   where 
p.ProductID = @ParentId and i.IsSelling = 1 and
(
@IsWebSite = 1 and i.ItemNumber like '%-NLA' or
@IsWebSite = 2 and i.ItemNumber like '%-NLA DEL' or
@IsWebSite = 3 and i.ItemNumber like '%-NLAmod'
)