doulai8405 2013-07-02 02:48
浏览 31
已采纳

从编程集体智能编程,第4章,这个查询的MySQL等价物是什么?

I'm reading the book Programming Collective Intelligence where all the examples are done in Python and sqllite. However, I'm doing all the examples in PHP / MYSQL.

Chapter 4, pages 63 and 64 discuss how to retrieve results from a search. Specifically, it retrieves document ids whose related texts contain all the words in the search phrase and returns that id along with the location of each word's location in the text.

Thankfully, these pages and code are available online for free for reference. Here is the query from page 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

Here is the example result set from the book where the first number is the ID of the document, and the second and third numbers represent the location of each word in that specific document.

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),..

I know nothing about databases, so this query is throwing me a bit. How would I write this in MySQL?

I'm confident in my PHP, and I know once I understand the SQL and how to construct it dynamically, I'll have no problem generating the PHP code for the getmatchrows() function. But, if you have a suggestion for the related getmatchrows PHP definition to accompany the SQL query, please share that.

  • 写回答

2条回答 默认 最新

  • duanou2016 2013-07-02 02:53
    关注

    That query will work, although the more modern syntax uses explicit joins:

    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
    

    with more words it would be:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启