2012-01-12 44 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'%' 
) 

注意

這可能是一個性能殺手