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条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来