duanhuiuw12555 2012-10-16 20:13
浏览 29

多项查询

I have a search engine which scans all the words in a given web page and then shows their occurrence. Then they are ranked by the ranked by the amount of occurance the word appears in the document. But it doesn't return multiple term queries.

Below is my SQL Query. I would like to be able to have it check all the words inputted and then rank by the amount of times the words appear in the document. It is only working for single term queries at the moment.

         $result = mysql_query(" SELECT p.page_url AS url,
                       COUNT(*) AS occurrences 
                       FROM page p, word w, occurrence o
                       WHERE p.page_id = o.page_id AND
                       w.word_id = o.word_id AND
                       w.word_word = \"$keyword\"
                       GROUP BY p.page_id
                       ORDER BY occurrences DESC
                       LIMIT $results" );
  • 写回答

2条回答 默认 最新

  • doudong3570 2012-10-16 20:30
    关注

    If you want to get all the words, then your join conditional will not allow you to do so

    w.word_word = \"$keyword\"
    

    Your query can be written as follows

    $sql = "SELECT p.page_url as url, COUNT(*) as occurences "
         . "FROM page p "
         . "INNER JOIN occurence o ON p.page_id = o.page_id "
         . "INNER JOIN word w ON w.word_id = o.word_id "
         . "GROUP BY p.page_id "
         . "ORDER BY occurences DESC "
         . "LIMIT {$results}";
    $result = mysql_query($sql);
    

    This will grab all the words in the word table thus providing you with the results that (as I understand) need.

    If you are interested in a few words then you can use the IN statement (as suggested by Dev in the comments) and your query will become:

    $my_keywords = array('apple', 'banana');
    // This produces: "apple", "banana" and assumes that all of your 
    // keywords are in lower case. If not, you can transform them to lower
    // case or if you don't want that, remove the LOWER() function below 
    // from the WHERE
    $keywords    = '"' . implode('","', $my_keywords) . '"';
    $sql = "SELECT p.page_url as url, COUNT(*) as occurences "
         . "FROM page p "
         . "INNER JOIN occurence o ON p.page_id = o.page_id "
         . "INNER JOIN word w ON w.word_id = o.word_id "
         . "WHERE LOWER(w.word_word) IN ({$keywords}) "
         . "GROUP BY p.page_id "
         . "ORDER BY occurences DESC "
         . "LIMIT {$results}";
    $result = mysql_query($sql);
    

    Finally, try using mysqli instead of mysql, or PDO.

    HTH

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP