2015-09-23 70 views
3

我使用的是SQL實現搜索,這是查詢SQL查詢功能獲得最匹配的參數

Select CM.ID, ProductName,ImageURL,SKU,AA.Name as 
MemberName,Price,Discount,DM.Name as CategoryName from tblMasterProduct CM 
INNER JOIN tblProducts OM ON CM.ID=OM.MasterProductID 
INNER JOIN tblMasterCategory DM ON CM.SubCategoryID=DM.ID 
INNER JOIN tblOnlineRetailMember AA ON OM.MemberID=AA.ID 
WHERE 1=1 AND CM.ProductName LIKE '% watches %' AND CM.SubCategoryID= 112 
AND (OM.Price - OM.Discount) BETWEEN 0 And 200000 

的問題是,包含該單詞的手錶,而我也該查詢返回結果喜歡獲取包含「Watch」一詞的所有條目。

編輯:對不完整的信息抱歉,查詢是寫入存儲過程和字符串由用戶輸入,無法從用戶獲得「觀察」。

+0

'%鐘錶%'(之前和之後的空間)例如'smartwatches''不會被退回。當你查詢'%watch%'時,你將獲得包含'watch'的所有內容。手錶,智能手錶,手錶,看,... – CeOnSql

回答

1

嘗試使用SOUNDEXDIFFERENCE功能MSSQL。如果ProductName是一個多話,那麼你可以使用PARSENAME拆分單詞並使用差異找到字符串中的一個類似的一句話:

select * from t WHERE DIFFERENCE(ProductName,'watches')>=3 

SQLfiddle demo

當您查詢
0

您可以使用OR ..

Select CM.ID, ProductName,ImageURL,SKU,AA.Name as 
MemberName,Price,Discount,DM.Name as CategoryName from tblMasterProduct CM 
INNER JOIN tblProducts OM ON CM.ID=OM.MasterProductID 
INNER JOIN tblMasterCategory DM ON CM.SubCategoryID=DM.ID 
INNER JOIN tblOnlineRetailMember AA ON OM.MemberID=AA.ID 
WHERE 1=1 AND (CM.ProductName LIKE '%watches%' or CM.ProductName LIKE '%watch%' ) AND CM.SubCategoryID= 112 
AND (OM.Price - OM.Discount) BETWEEN 0 And 200000 
+0

請再次檢查編輯的問題。 – user2417160

0

可以通過OR實現它。

查詢

Select CM.ID, ProductName,ImageURL,SKU,AA.Name as MemberName, 
Price,Discount,DM.Name as CategoryName from tblMasterProduct CM 
INNER JOIN tblProducts OM ON CM.ID=OM.MasterProductID 
INNER JOIN tblMasterCategory DM ON CM.SubCategoryID=DM.ID 
INNER JOIN tblOnlineRetailMember AA ON OM.MemberID=AA.ID 
WHERE 1=1 AND (CM.ProductName LIKE '%watches%' OR CM.ProductName LIKE '%watch%') 
AND CM.SubCategoryID= 112 
AND (OM.Price - OM.Discount) BETWEEN 0 And 200000; 
+0

請再次檢查已編輯的問題。 – user2417160

+0

@ user2417160:用戶輸入手錶? – Wanderer

+0

是用戶正在輸入手錶。 – user2417160

0

如果您正在尋找動態查詢,這個工程

Select CM.ID, ProductName,ImageURL,SKU,AA.Name as MemberName, 
Price,Discount,DM.Name as CategoryName from tblMasterProduct CM 
INNER JOIN tblProducts OM ON CM.ID=OM.MasterProductID 
INNER JOIN tblMasterCategory DM ON CM.SubCategoryID=DM.ID 
INNER JOIN tblOnlineRetailMember AA ON OM.MemberID=AA.ID 
WHERE 1=1 AND (CM.ProductName LIKE '%watches %' 
OR CHARINDEX(LEFT('watches',len('watches')-1) ,CM.ProductName) > 0 --watche 
OR CHARINDEX(LEFT('watches',len('watches')-2) ,CM.ProductName) > 0 --watch 
) 
AND CM.SubCategoryID= 112 
AND (OM.Price - OM.Discount) BETWEEN 0 And 200000; 
+0

該查詢不返回任何結果。 – user2417160

+0

我已經刪除了左側LIKE'%watches%'的空間,請添加空格並嘗試,--------- LIKE'%watches%'------------ – Prashant