【问题标题】:How to use LIKE statement with multiple values from another field?如何将 LIKE 语句与来自另一个字段的多个值一起使用?
【发布时间】:2015-03-21 10:28:43
【问题描述】:

我想选择字段包含来自另一个字段的值的所有记录。我怎么做?这是我正在尝试的代码。

select field1 , field2, field3 
from table1
where field1 like '%'+(select distinct field4 from table2)+'%'


提前致谢。

【问题讨论】:

    标签: sql sql-server-2008 compare sql-like


    【解决方案1】:

    只要你喜欢作为加入条件:

    select field1 , field2, field3 
    from table1
    join (select distinct field4 from table2) x
      on field1 like '%'+field4+'%'
    

    【讨论】:

    • 感谢您的帮助。
    【解决方案2】:

    使用查询的原始结构,您可以:

    select field1, field2, field3 
    from table1 t1
    where exists (select 1
                  from table2
                  where t1.field1 like '%' + field4 + '%'
                 );
    

    这种方法的优点是不会重复table1中的记录。例如,如果table2 中有两行的值分别为'a''b'table1 中有一行的值为'ab',则此方法将只返回 table1 中的行一次。

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 2021-09-13
      • 2011-05-24
      • 1970-01-01
      相关资源
      最近更新 更多