想法是按短语获取搜索结果(搜索几列),排序优先级必须取决于匹配的列(包括完全匹配的情况)。
我的示例运行良好,但我仍然认为有更好的方法:
WITH products
AS ( SELECT
-- .. some columns
--------- For Sorting -------------
[sortOrder] = CASE
WHEN [partNumber] = @searchPhrase
OR [manPartNumber]= @searchPhrase
THEN 1
WHEN FormatSiteNameForSearch( [siteName] )
LIKE '%' + @searchPhrase + '%'
THEN 2
WHEN [partNumber] LIKE '%' + @searchPhrase + '%'
OR [manPartNumber] LIKE '%' + @searchPhrase + '%'
THEN 3
ELSE 100
END
--------- End For Sorting -------------
FROM [dbo].[PRODUCTS_Products]
WHERE
[partNumber] LIKE '%' + @searchPhrase + '%'
OR [manPartNumber] LIKE '%'+ @searchPhrase + '%'
OR FormatSiteNameForSearch([siteName]) LIKE '%'+@searchPhrase+'%'
OR [name] LIKE '%'+ @searchPhrase+ '%'
)
SELECT TOP(10) *
FROM products
ORDER BY sortOrder
谢谢你。