我有简单的 tbl_posts 表,其中有三列(id,name,content)
我为名称和内容列创建了全文索引。 当我查询它时:
SELECT *
FROM dbo.tbl_posts
WHERE FREETEXT ( *, 'search word' )
我希望结果订单首先按名称列中的排名然后内容
排序答案 0 :(得分:1)
CREATE FUNCTION PostFreeTextSearch
(
@SearchTerms nvarchar(100)
)
RETURNS TABLE
AS
RETURN
(
SELECT
CASE WHEN fttName.[Key] IS NULL THEN fttContent.[Key] ELSE fttName.[Key] END as id,
fttName.[RANK] as NameScore,
fttContent.[RANK] as ContentScore
FROM
FREETEXTTABLE(tbl_Posts, (Name), @SearchTerms) fttName
FULL OUTER JOIN
FREETEXTTABLE(tbl_Posts, (Content), @SearchTerms) fttContent ON fttName.[Key] = fttContent.[Key]
WHERE fttName.RANK > 0 OR fttContent.RANK > 0
)
GO
SELECT * FROM PostFreeTextSearch('sql server')