0

私は、すべての例が Python と sqllite で行われている本Programming Collective Intelligenceを読んでいます。ただし、PHP / MYSQL ですべての例を実行しています。

第 4 章の 63 ページと 64 ページでは、検索結果を取得する方法について説明しています。具体的には、関連するテキストに検索フレーズ内のすべての単語が含まれるドキュメント ID を取得し、テキスト内の各単語の位置とともにその ID を返します。

ありがたいことに、これらのページとコードはオンラインで無料で参照できます。64ページからのクエリは次のとおりです。

select w0.urlid,w0.location,w1.location
from wordlocation w0,wordlocation w1
where w0.urlid=w1.urlid
and w0.wordid=10
and w1.wordid=17

以下は、本からの結果セットの例です。最初の数字はドキュメントの ID で、2 番目と 3 番目の数字は、その特定のドキュメント内の各単語の位置を表します。

e.getmatchrows('functional programming')
([(1, 327, 23), (1, 327, 162), (1, 327, 243), (1, 327, 261),
(1, 327, 269), (1, 327, 436), (1, 327, 953),..

私はデータベースについて何も知らないので、このクエリには少し戸惑います。これを MySQL でどのように記述しますか?

私は自分の PHP に自信を持っています。SQL とそれを動的に構築する方法を理解すれば、getmatchrows()関数の PHP コードを問題なく生成できます。ただし、関連する getmatchrows PHP 定義を SQL クエリに追加するための提案がある場合は、それを共有してください。

4

2 に答える 2

2

このクエリは機能しますが、より新しい構文では明示的な結合が使用されます。

SELECT w0.urlid, w0.location, w1.location
FROM wordlocation w0
JOIN wordlocation w1 ON w0.urlid = w1.urlid
WHERE w0.wordid = 10
AND w1.wordid = 17

さらに言葉を加えると、次のようになります。

SELECT w0.urlid, w0.location, w1.location, w2.location, w3.location, w4.location
FROM wordlocation w0
JOIN wordlocation w1 ON w0.urlid = w1.urlid
JOIN wordlocation w2 ON w0.urlid = w2.urlid
JOIN wordlocation w3 ON w0.urlid = w3.urlid
JOIN wordlocation w4 ON w0.urlid = w4.urlid
WHERE w0.wordid = 10
AND w1.wordid = 17
AND w2.wordid = 101
AND w3.wordid = 25
AND w4.wordid = 4
于 2013-07-02T02:53:37.517 に答える
1
select w0.urlid,w0.location,w1.location
from wordlocation w0,wordlocation w1
where w0.urlid=w1.urlid
and w0.wordid=10
and w1.wordid=17

は有効な mySQL クエリであり、問​​題なく動作するはずです。

于 2013-07-02T02:53:54.620 に答える