0
SELECT A.a, B.b, akt.[Rank] + bkt.[Rank] /2  AS [Rank]
FROM B b
INNER JOIN Publication a ON a.Id = b.Id
INNER JOIN FREETEXTTABLE(A, a, 'search text') akt ON a.Id = akt.[Key]
INNER JOIN FREETEXTTABLE(B, b, 'search text') bkt ON b.Id = bkt.[Key] 
ORDER BY [Rank] DESC

UNION
SELECT A.a, null as B.b, akt.[Rank] as [Rank]
FROM A a
INNER JOIN FREETEXTTABLE(A, a, 'search text') akt ON a.Id = akt.[Key]

UNION
SELECT null as A.a, B.b, bkt.[Rank] as [Rank]
FROM B b
INNER JOIN FREETEXTTABLE(A, a, 'search text') akt ON a.Id = akt.[Key]

上述查询用于跨两个表搜索记录(使用排名)。

第一个查询:只有搜索文本在两个表的列中的那些记录才会显示。

第二个查询:只有那些 searchtext 只在表 A 的列中的记录

第三个查询: 仅那些 searchtext 仅在表 B 的列 b 中的记录

我的问题是:如果我必须搜索 4 或 5 个表,UNIONS 的数量会疯狂增加。它也会太复杂和缓慢。

那么,有没有其他方法可以减少这些联合?我试过视图,但它们不能被全文索引。

4

2 回答 2

0

Simply use full join

SELECT A.a, B.b, "Use case for calculating rank"
    FROM B b
    FULL JOIN A a ON b.Id = a.Id
WHERE
    a.[columnname] = 'search text' OR
    b.[columnname] = 'search text' 

Check http://codesnout.com/SQLSample/SQL-FULL-JOIN.php

于 2012-07-27T06:23:14.260 回答
0
SELECT b.FK_Publication_ID, b.PageNumber as PageNumber, b.SearchText as SearchText, a.Title as Title , 
TitleSearch.[Rank] + PubSearch.[Rank] * 10000
  AS [Rank]
FROM PublicationSearch 
INNER JOIN Publication a ON b.FK_Publication_Id = a.Id
INNER JOIN FREETEXTTABLE(PublicationSearch, SearchText, "searchtext") PubSearch ON b.Id = PubSearch.[Key] 
INNER JOIN FREETEXTTABLE(Publication, Title, "searchtext") TitleSearch ON a.Id = TitleSearch.[Key] 
WHERE b.FK_ContentType_Id IN (SELECT * FROM UF_CSVToTable(@Options))

UNION 


SELECT  a.Id, null as PageNumber, null as Searchtext, a.Title as Title, TitleSearch.[Rank] * 100  AS [Rank]
FROM Publication a
INNER JOIN FREETEXTTABLE(Publication,Title, "searchtext") TitleSearch ON a.Id = TitleSearch.[Key]


UNION

SELECT b.FK_Publication_ID, b.PageNumber as PageNumber, b.SearchText as SearchText, null as Title, 
PubSearch.[Rank] 
  AS [Rank] 
FROM PublicationSearch b
INNER JOIN FREETEXTTABLE(PublicationSearch, SearchText, "searchtext") PubSearch ON b.Id = PubSearch.[Key] 

ORDER BY [Rank] DESC

以上是使用UNION

下面是你建议的

选择 b.FK_Publication_ID,b.PageNumber 作为 PageNumber,b.SearchText 作为 SearchText,a.Title 作为 Title 作为 ContentType,

TitleSearch.[Rank] * 100  (Ranking, dont know how to do it here)
  AS [Rank]
FROM PublicationSearch b
INNER JOIN Publication a ON b.FK_Publication_Id = a.Id
JOIN FREETEXTTABLE(PublicationSearch, SearchText, "searchtext")PubSearch ON b.Id = PubSearch.[Key] 
FULL JOIN FREETEXTTABLE(Publication, Title, "searchtext")TitleSearch ON a.Id = TitleSearch.[Key] 

order by [RANK] desc

继承人,确切的脚本。使用完全联接,我从 BOTH 表和仅从 PublicationSearch 表中获取记录,但不仅从 Publication 表中获取记录

此外,在两个表中找到的记录应该排名更高,然后是仅来自 Publication 表的记录,然后是来自 PublicationSerach 表的记录

谢谢..

于 2012-07-27T08:23:43.813 回答