【问题标题】:'LIKE' clause with 'AND' operator in SQL Server 2012SQL Server 2012 中带有“AND”运算符的“LIKE”子句
【发布时间】:2016-10-03 06:49:26
【问题描述】:

我的要求与此线程中的要求完全相同。 How to use JOIN LIKE with AND Operator in SQL Server?

尽管在网上搜索了几个小时,我仍然一无所知。

我有一个表“过滤器”,其中列“值”和 2 条记录作为“值 1”,“值 2”作为 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%'

谁能告诉我一条出路。

【问题讨论】:

    标签: sql-server join sql-like


    【解决方案1】:

    您通常可以将“X 匹配所有 Y”查询重写为“X 没有任何不匹配的 Y” - 或者换句话说,使用 NOT EXISTS 查询 Y 的逆。

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

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多