如何执行multiquery fql语句?

时间:2012-12-01 00:28:23

标签: python sql facebook json facebook-fql

从下面的FQL查询中,我可以获取我的新闻源上的帖子的actor_id和附件数据,并包含“www.youtube.com”。但是,我希望能够获得该用户的图片和名称。我如何将其纳入以下查询?

SELECT created_time, post_id, actor_id, type, updated_time, attachment
FROM stream 
WHERE post_id IN 
   (SELECT post_id
    FROM stream
    WHERE ('video') IN attachment 
    AND source_id IN
       (SELECT uid2
        FROM friend
        WHERE uid1=me())
    LIMIT 100)

编辑:

    #right fql query, but prints only part of it
    posts_query = "SELECT created_time, post_id, actor_id, type, updated_time, attachment FROM stream WHERE post_id in (select post_id from stream where ('video') in attachment AND source_id IN ( SELECT uid2 FROM friend WHERE uid1=me()) limit 100)"
    fql_var = "https://api.facebook.com/method/fql.query?access_token=" + token['access_token'] + "&query=" + posts_query + "&format=json"
    data = urllib.urlopen(fql_var)
    fb_stream = json.loads(data.read())
    #print ast.literal_eval(json.dumps(fb_stream))

    users_query = "SELECT uid, first_name FROM user WHERE uid IN (SELECT actor_id FROM (" + posts_query+"))"
    fqlquery_user = "https://api.facebook.com/method/fql.query?access_token=" + token['access_token'] + "&query=" + users_query + "&format=json"
    user_data2 = urllib.urlopen(fqlquery_user)
    user_stream2 = json.loads(user_data2.read())
    print ast.literal_eval(json.dumps(user_stream2))

2 个答案:

答案 0 :(得分:1)

你需要使用FQL Multiquery,这样你才能有两个查询 - 你自己获取“帖子”而另一个查询“演员”。查询一起执行,您必须调整代码以配对使用actor_id值返回的两个数组。

"posts":"your_query_here"
"actors":"SELECT name,pic_square FROM user WHERE uid IN (SELECT actor_id FROM #posts)"

You can see the results of this query here

答案 1 :(得分:1)

似乎只能通过一个查询来执行此操作,因为:

  1. 在FQL中,您只能从一个表中SELECT
  2. FQL中没有JOIN。 (请参阅this questionthat one
  3. 如果可以,这就是它的样子:

    SELECT created_time, post_id, actor_id, type, updated_time, attachment, user.first_name
    FROM stream
    WHERE ('video') in attachment AND 
            source_id IN (SELECT uid2 FROM friend WHERE uid1=me())
    LEFT JOIN user ON user.uid=actor_id
    LIMIT 100
    

    但是没有联接,所以你需要使用Facebook所谓的multiquery,你会有两个问题:

    posts_query =
    SELECT created_time, post_id, actor_id, type, updated_time, attachment
    FROM stream
    WHERE ('video') in attachment AND 
            source_id IN (SELECT uid2 FROM friend WHERE uid1=me())
    LIMIT 100
    
    users_query =
    SELECT uid, first_name
    FROM user
    WHERE uid IN (SELECT actor_id FROM #posts_query)
    

    以下是Explorer的链接供您测试。

    注意:在原始查询中有一些无用的东西,它从表中进行选择,并在同一个表的where子句中添加子查询。所以这是多余的。

    <强>更新

    #right fql query, but prints only part of it
    posts_query = "SELECT created_time, post_id, actor_id, type, updated_time, attachment FROM stream WHERE post_id in (select post_id from stream where ('video') in attachment AND source_id IN ( SELECT uid2 FROM friend WHERE uid1=me()) limit 100)"
    users_query = "SELECT uid, first_name FROM user WHERE uid IN (SELECT actor_id FROM (#posts_query))"
    
    queries = {'posts_query': posts_query, 'users_query': users_query}
    fql_var = "https://api.facebook.com/method/fql.query?access_token=" + token['access_token'] + "&q=" + json.dumps(queries_json) + "&format=json"
    data = urllib.urlopen(fql_var)
    fb_stream = json.loads(data.read())
    print ast.literal_eval(json.dumps(fb_stream))
    

    我们的想法是将两个查询的名称作为键发送,以JSON格式编码,以便Facebook了解#posts_query是您在同一API请求中发送的那个。您完全按照在JSON编码对象中发送它的名称发送名称,而不是由查询本身替换它。有关详细信息和示例,请查看有关多查询的文档。