1

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

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

2 回答 2

1

似乎你不能只用一个查询来做到这一点,因为:

  1. SELECT在 FQL 中一次只能从一个表中。
  2. FQL 中没有 JOIN。(见这个问题那个问题)

如果可以的话,它会是这样的:

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)

这是资源管理器的链接,供您对其进行测试。

注意:您在原始查询中做了一些无用的事情,即从表中选择,并在同一个表的 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 编码对象中发送的名称发送名称,而不是用查询本身替换它。查看有关 multiquery 的文档以获取详细信息和示例。

于 2012-12-06T08:52:23.600 回答
1

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

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

您可以在此处查看此查询的结果

于 2012-12-06T08:48:52.530 回答