3

次の3つのテーブルがあります。

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

単語テーブルには、すべてのドキュメントで発生したすべての単語が含まれ、word_documentは、単語をドキュメントに関連付け、そのドキュメント内のその単語の数を関連付けます。

2つの単語を検索するクエリを作成し、ドキュメント内の両方の単語の数の合計で順序付けられた両方の単語を持つドキュメントのみを返します。

例えば

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

アップルブルーを検索すると、次のようになります。

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

交差を正常に使用しましたが、カウントの合計と順序が返されません。

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 に答える