Find a photo for each of 10 random Facebook friends in FQL?
I'm attempting to开发者_运维百科 do a Facebook FQL Multiquery (via JS SDK FB.api call) to load a single photo for each of 10 random Facebook friends where they have been tagged (aka "Show 10 random friends and a photo of theirs"). Here is my Javascript array of queries:
var queries = {
q1:"SELECT uid, name FROM user WHERE uid IN (Select uid2 from friend where uid1 = " + user_id
+ " order by rand() limit 10) ",
q2: "SELECT pid, subject from photo_tag where subject in (SELECT uid from #q1) limit 10",
q3:"SELECT src from photo where pid in (SELECT pid from #q2)"};
What I was trying to do:
- Query 1: Get the userid and name of 10 random friends of the user.
- Query 2: Get the photo id and subject name of phototags where the user ids matched those from query 1.
- Query 3: Select the image src from the photos that matched those in query 2.
The problem is that I have no way in Query 2 to select DISTINCT records by users. That is, I cannot tell Facebook to return just a single photo for each user. Right now, it is arbitrary and all 10 rows could be different photo tags of the same user.
I could do some looping in Javascript and make a single FQL query for a pic for each matched user, but that just seems wrong. Any other suggestions on how to do this efficiently, preferably directly in FQL?
Thanks!
Well, I solved this awhile back and am closing this for completeness.
What I had to do was NOT do it all in a single multiquery. I had to FIRST make a single query to retrieve some random friends, THEN make a multiquery where we load a random photo for each of these friends.
FB.api(
{
method: 'fql.query',
query: "SELECT uid, name FROM user WHERE uid IN (Select uid2 from friend where uid1 = " + user_id
+ " order by rand() limit 8) "
}, function (response) {
var queries = {};
for (x in response) {
if (typeof photoArray[response[x].uid] == 'undefined') photoArray[response[x].uid] = [];
photoArray[response[x].uid]['name'] = response[x].name;
queries[response[x].uid] = "SELECT src_big, caption from photo where pid in (SELECT pid from photo_tag where subject =" + response[x].uid + " order by rand() limit 1) ";
}
FB.api(
{
method: 'fql.multiquery',
queries: queries
},
function(response) {
for (y in response) {
if (typeof response[y].fql_result_set[0] != 'undefined') {
photoArray[response[y].name]['image_src'] = response[y].fql_result_set[0].src_big;
// Do what you want with the pics
}
}
精彩评论