SQL:将"in(...)"解释为"like"

mar*_*osh 3 sql

我有一个像下面的SQL选择:

select * from table1 where text in (select text from table2)
Run Code Online (Sandbox Code Playgroud)

clausule中的实际更复杂选择这一点.text是字符串(varchar).如何展开SQL选择行从table1这里text 就像是从文本table2(不只是恰好等于)?

Mik*_*son 6

如果在表2的文本列中有通配符表达式,则可以这样做.

select *
from Table1 as T1
where exists (select *
              from Table2 as T2
              where T1.[text] like T2.[text])
Run Code Online (Sandbox Code Playgroud)

否则,您需要%在查询中添加.

select *
from Table1 as T1
where exists (select *
              from Table2 as T2
              where T1.[text] like '%'+T2.[text]+'%')
Run Code Online (Sandbox Code Playgroud)