2

在 ms sql 2012 服务器中,我在几列上有一个全文搜索索引。
我使用 FREETEXTTABLE 或 CONTAINSTABLE 进行查询,如果搜索文本出现在多列而不是其他列,我的反应应该不同。
有没有办法区分或接收结果出现的列名?
谢谢。

4

1 回答 1

1

我假设您调用 FREETEXTTABLE/CONTAINSTABLE 时可以*选择在所有列中进行搜索。相反,请尝试进行多次调用,每列一个。您可以在 1 个查询中执行此操作,也可以根据具体情况作为单独的查询执行此操作。

例如(1-查询方法):

SELECT
   Case
      When (MatchesColumn1or2.[KEY] is not null) Then 'Matches Column 1 or 2'
      When (MatchesColumn3.[KEY] is not null) Then 'Matches Column 3'
   End
FROM
   FREETEXTTABLE(MyTable, (Column1, Column2), 'cat AND dog') as MatchesColumn1or2
full outer join
   FREETEXTTABLE(MyTable, Column3, 'cat AND dog') as MatchesColumn3
   on (MatchesColumn1or2.[KEY] = MatchesColumn3.[KEY])

这是一个示例,如果您想保留原始排名值,但请注意增加的性能损失:

SELECT
   MatchAllColumns.[RANK],
   Case
      When (MatchesColumn1or2.[KEY] is not null) Then 'Matches Column 1 or 2'
      When (MatchesColumn3.[KEY] is not null) Then 'Matches Column 3'
   End
FROM
   FREETEXTTABLE(MyTable, *, 'cat AND dog') as MatchAllColumns
left join
   FREETEXTTABLE(MyTable, (Column1, Column2), 'cat AND dog') as MatchesColumn1or2
   on (MatchAllColumns.[KEY] = MatchesColumn1or2.[KEY])
left join
   FREETEXTTABLE(MyTable, Column3, 'cat AND dog') as MatchesColumn3
   on (MatchAllColumns.[KEY] = MatchesColumn3.[KEY])
于 2013-06-10T14:26:41.920 回答