全文搜索-是否有方法获取导致查询行的列名
本文关键字:查询 获取 有方法 文搜索 搜索 是否 | 更新日期: 2023-09-27 18:17:55
在ms sql 2012服务器中,我有一个全文搜索索引超过几列。
我查询使用freetextttable或CONTAINSTABLE和我的反应应该是不同的,如果搜索文本出现在几个列比在其他。
是否有一种方法来区分或接收显示结果的列名?
谢谢。
我假设您使用*
选项调用freetextttable/CONTAINSTABLE来搜索所有列。相反,请尝试多次调用,每个列调用一次。您可以在一个查询中执行此操作,也可以根据具体情况作为单独的查询执行此操作。
例如(1-query方法):
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])