在 SQL Server 2012 中带有 'AND' 运算符的 'LIKE' 子句
'LIKE' clause with 'AND' operator in SQL Server 2012
我的要求与此线程中的要求完全相同。 How to use JOIN LIKE with AND Operator in SQL Server?
尽管在网上搜索了几个小时,我还是一无所知。
我有一个 table 'Filter' 列 'Value' 和 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 LIKE 的意思是 AND 而不是 OR?类似于:
Select * From MasterTable
Where MasterTable.Name LIKE '%Value1%' AND MasterTable.Name LIKE '%Value2%'
谁能告诉我一条出路。
您通常可以将 "X matches all Y" 查询重写为 "X does not have any Y that do not match" - 或者换句话说,NOT EXISTS
查询 Y 的倒数。
select *
from MasterTable
where not exists (
select 1
from Filter
where MasterTable.Name not like '%' + Filter.Value + '%'
)
我的要求与此线程中的要求完全相同。 How to use JOIN LIKE with AND Operator in SQL Server?
尽管在网上搜索了几个小时,我还是一无所知。
我有一个 table 'Filter' 列 'Value' 和 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 LIKE 的意思是 AND 而不是 OR?类似于:
Select * From MasterTable
Where MasterTable.Name LIKE '%Value1%' AND MasterTable.Name LIKE '%Value2%'
谁能告诉我一条出路。
您通常可以将 "X matches all Y" 查询重写为 "X does not have any Y that do not match" - 或者换句话说,NOT EXISTS
查询 Y 的倒数。
select *
from MasterTable
where not exists (
select 1
from Filter
where MasterTable.Name not like '%' + Filter.Value + '%'
)