2011-11-08 59 views
2

我使用以下查詢來獲取包含inventoryLocalization表的名稱列中的一些詞(由函數分割)的所有數據。具有多個搜索詞的sql查詢

在這個例子中,我分割了「紅綠藍」字符串。它應該是 ,它返回所有行,如運算符。

SELECT distinct 
inL.name 
FROM dbo.[inventoryLocalization] inL 
JOIN fnSplitString (N'red green blue',' ') words ON (inL.name LIKE '%'+ words.item +'%') 

我的問題是,是否有可能得到行擁有的所有單詞,如AND運營商。

回答

3
select inL.name from dbo.[inventoryLocalization] inL 
where not exists 
    (select 1 from fnSplitString(N'red green blue',' ') words 
    where (inL.name NOT LIKE '%'+ words.item +'%')) 
0

嘗試這樣:

DECLARE @SomeWords NVARCHAR(200), @Num INT 

SET @SomeWords = 'red green blue' 

SELECT @Num = COUNT(*) 
FROM fnSplitString (@SomeWords,' ') 

SELECT inL.name 
FROM dbo.[inventoryLocalization] inL 
JOIN fnSplitString (@SomeWords,' ')words 
ON (inL.name LIKE '%'+ words.item +'%') 
GROUP BY inL.name 
HAVING COUNT(*) = @Num