3

我有三个表如下:

documents (id, content) 
words (id, word) 
word_document (word_id, document_id, count)

单词表包含所有文档中出现的所有单词,word_document 将单词与文档以及该文档中该单词的计数相关联。

我想编写一个查询来搜索两个单词,并只返回两个单词都按文档中两个单词的计数总和排序的文档。

例如

DocA: green apple is not blue
DocB: blue apple is blue
DocC: red apple is red

现在搜索appleblue返回:

DocA, 3
DocB, 2

因为:

DocA contains both words and 3 of them
DocB contains both words and 2 of them
DocC only contains one word

我成功使用了intersect但它不返回计数和也没有顺序。

4

2 回答 2

0

我认为应该这样做:

select a.document_id, a.count + b.count
from 
(
 select document_id, count
 from word_document
 where word_id = 'apple'
 group by document_id
) a 
INNER JOIN 
(
 select document_id, count
 from word_document
 where word_id = 'blue'
 group by document_id
) b 
ON a.document_id = b.document_id
ORDER BY a.count + b.count
于 2012-12-14T18:04:48.613 回答
0

对于那些想要这个的人,这只适用于:

select wd.document_id, (wd.count + d.count) as tcount from word_document as wd
join words as w on w.id = wd.word_id
join
(select document_id, count from word_document 
join words on words.id = word_document.word_id
where words.word = "apple") d on d.document_id=wd.document_id
where w.word = "blue" order by tcount desc

您可以从内部查询创建临时表并在其上执行外部。它可以递归地完成更多的单词。

于 2012-12-14T21:03:52.803 回答