2012-01-12 87 views
3

我有一个SQL SELECT象下面这样:在在clausuleSQL:expading “在(...)” “喜欢”

select * from table1 where text in (select text from table2) 

在现实是更复杂的选择,这。 text是字符串(varchar)。如何扩展该sql以从table1中选择行,其中text就像来自table2的文本(不仅完全等于)?

回答

6

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

select * 
from Table1 as T1 
where exists (select * 
       from Table2 as T2 
       where T1.[text] like T2.[text]) 

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

select * 
from Table1 as T1 
where exists (select * 
       from Table2 as T2 
       where T1.[text] like '%'+T2.[text]+'%') 
1

事情是这样的:

select * 
from table1 
where exists 
    (
    select * 
    from table2 
    where table1.text like N'%'+table2.text+N'%' 
) 

注意

这可能是一个性能杀手