2016-10-03 95 views
0

我與本線程中的要求完全相同。 How to use JOIN LIKE with AND Operator in SQL Server?在SQL Server 2012中使用'AND'運算符的'LIKE'子句

儘管在網上查了幾個小時,但我一直無能爲力。

我有一個表'過濾器'列'值'和2記錄它作爲'Value1'和'Value2'作爲varchar值。

Select * From MasterTable 
Inner Join Filter ON MasterTable.Name LIKE '%' + Filter.Value + '%' 

這意味着查詢:

Select * From MasterTable 
Where MasterTable.Name LIKE '%Value1%' OR MasterTable.Name LIKE '%Value2%' 

現在,我怎麼改變JOIN類似的手段,而不是OR?例如:

Select * From MasterTable 
Where MasterTable.Name LIKE '%Value1%' AND MasterTable.Name LIKE '%Value2%' 

任何人都可以告訴我一個出路。

回答

2

通常你可以重寫「X匹配所有Y」查詢爲「X沒有不匹配任何Y」 - 或者換句話說,一個NOT EXISTS查詢Y.

select * 
from MasterTable 
where not exists (
    select 1 
    from Filter 
    where MasterTable.Name not like '%' + Filter.Value + '%' 
) 
的倒數