组合多个WHERE子句

时间:2016-10-08 23:00:23

标签: sql-server tsql

我有以下适用于我的选择语句。

select * 
from BND_listing right 
join BND_ListingCategories on BND_Listing.CatID = BND_ListingCategories.CatID
where 
    (CategoryName = '[querystring:filter-Category]' or 
     '[querystring:filter-Category]' = 'All')
    and (City = '[querystring:filter-City]' or 
         '[querystring:filter-City]' = 'All')
    and (Region = '[querystring:filter-State]' or 
         '[querystring:filter-State]' = 'All')
    and (Country = '[querystring:filter-Country]' or 
         '[querystring:filter-Country]' = 'All')
    and isnull(Company,'') <> ''
order by 
    Company asc

除了它正在做的事情之外,我还想包括以下WHERE子句,它基本上只允许我创建一个&#34;搜索框&#34;

DECLARE @param VARCHAR(MAX)
SET @param = '[querystring:searchterm]'

SELECT Company
FROM BND_Listing
WHERE Company LIKE '%' + @param + '%'

是否可以将这两个select语句合并为一个并保留所有功能?

1 个答案:

答案 0 :(得分:1)

DECLARE @param VARCHAR(MAX)
SET @param = '[querystring:searchterm]'


select * 
from BND_listing right 
join BND_ListingCategories on BND_Listing.CatID = BND_ListingCategories.CatID
where 
    (CategoryName = '[querystring:filter-Category]' or 
     '[querystring:filter-Category]' = 'All')
    and (City = '[querystring:filter-City]' or 
         '[querystring:filter-City]' = 'All')
    and (Region = '[querystring:filter-State]' or 
         '[querystring:filter-State]' = 'All')
    and (Country = '[querystring:filter-Country]' or 
         '[querystring:filter-Country]' = 'All')
    and isnull(Company,'') <> ''
    AND Company LIKE '%' + @param + '%'
order by 
    Company asc