FQL substitute for an SQL JOIN when I need related data from both joined tables? - facebook-fql

I am trying to get a list of friends who have liked any of a user's statuses in the past. I have a multiquery below that will do this, but the results are not grouped by post. So, I get a list of all friends who have liked the posts, but I don't know which friends liked which posts. I know I can run a separate call for each status, but I would like to do this in one call if possible. Any ideas?
$queries = '{
"statuses":"SELECT status_id FROM status WHERE uid=me()",
"likes":"SELECT user_id FROM like where object_id IN ( SELECT status_id FROM #statuses )",
"liker":"SELECT name, pic_small FROM user WHERE uid IN (SELECT user_id FROM #likes)"
}';
$param = array(
'method' => 'fql.multiquery',
'queries' => $queries,
'callback' => '');
$queryresults = $facebook->api($param);

I would've thought that you could just do:
$queries = '{
"statuses":"SELECT status_id FROM status WHERE uid=me()",
"likes":"SELECT user_id, object_id FROM like where object_id IN ( SELECT status_id FROM #statuses )",
"liker":"SELECT name, pic_small, uid FROM user WHERE uid IN (SELECT user_id FROM #likes)"
}';
Then on your end you'll have all the information you need to map each status_id to an array of names. It's not as clean or as trivial as doing an SQL JOIN, but since that's not an option this is the best you can do if you want to get all the data in one FQL call.

Related

Facebook fql search by first letter of the name

Hello guys i'm trying to do a fql query that search the friends by first letter of the name, i did this query but doesn't show me the right result.
In this case i tried to get all my friends with the first letter "g" but CONTAINS doens't do what i'm looking for. There is another way?
SELECT uid , first_name, last_name FROM user WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me()) AND ( CONTAINS('g')
AND strpos(lower(name),lower('g')) >=0 ) ORDER BY first_name ASC
The problem is with your usage of strpos(). You query will retrieve all your friends having a letter 'g' in their name. And, I think there's no point of using a contains() here.
This query will work fine for your task:
SELECT uid , first_name, last_name FROM user WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me()) AND
strpos(lower(first_name),lower('g')) = 0 ORDER BY first_name ASC
As you can see here, I'm using strpos(lower(first_name),lower('g')) = 0. This will retrieve only those friends having 'g' as the first letter in their first name.

FQL not returning photos tagged with friend

I am trying to use FQL to get a list of my own photos that have friends tagged in them. The friends are specified by their name or part of their name. i.e input from a text box
So far I have come up with the FQL below, which almost works, but...
Can anyone tell me why the first query returns a list of records but the second query returns nothing? the only difference between the two is the second one searches on just part of the person's name i.e "laura" vs "lau"
FQL 1
SELECT object_id, src_big, src_big_width, src_big_height, aid
FROM photo
WHERE object_id in (
SELECT object_id
FROM photo_tag
WHERE subject in (
SELECT uid
FROM user
WHERE (strpos(lower(name),"laura") >=0 )
AND (uid in (SELECT uid2 FROM friend WHERE uid1 = me()) ))
)
AND owner = me()
FQL 2
SELECT object_id, src_big, src_big_width, src_big_height, aid
FROM photo
WHERE object_id in (
SELECT object_id
FROM photo_tag
WHERE subject in (
SELECT uid
FROM user
WHERE (strpos(lower(name),"lau") >=0 )
AND (uid in (SELECT uid2 FROM friend WHERE uid1 = me()) ))
)
AND owner = me()
It looks like Facebook has a bug in its system so strpos doesn't work as expected in sub queries. As a workaround you could try splitting it into two queries - one that finds list of id-s:
SELECT uid
FROM user
WHERE (strpos(lower(name),"lau") >=0 )
AND (uid in (SELECT uid2 FROM friend WHERE uid1 = me()) )
And a second one that uses this list:
SELECT object_id, src_big, src_big_width, src_big_height, aid
FROM photo
WHERE object_id in (
SELECT object_id
FROM photo_tag
WHERE subject in (111,222,333,444,555)
)
AND owner = me()

FQL Query for getting Chat conversation

Here is the code. In the result I want to see my conversation and my friend conversation. For conversation history functionality How can I do this?
SELECT message_id, thread_id, author_id, body, created_time, viewer_id
FROM message WHERE thread_id IN
(SELECT thread_id, subject, recipients FROM thread WHERE folder_id =0 )
AND author_id = 'xxxxxxxxxxxxxxx' ORDER BY created_time DESC LIMIT 0,25
This code returns only my friend data.
You've restricted the comments returned to those authored by your friend only.
Change
AND author_id = FRIEND_ID
to
AND (author_id = FRIEND_ID OR author_id = me())
in the `WHERE clause of your query.
Get a thread_id by using your original query and then try this fql query. I think a thread_id belongs to whole conversation
SELECT message_id, thread_id,source,author_id,body,created_time,viewer_id FROM
message WHERE thread_id=THREAD_id AND (author_id=FRIEND_ID OR
author_id=me() ) ORDER BY created_time DESC LIMIT 0,25

FQL multi-query optmization for events listing

I am working on a custom Events app in Facebook and have ran into an issue where the response seems quite slow. The slowness mostly relate to trying to search for a page that is associated with an events geographical location. I.e "City, Country". Once such URL would be:
http://www.facebook.com/pages/Buenos-Aires-Argentina/106423786059675
I've already determined that it is possible to obtain the page URL for a page like this with the following FQL query:
SELECT page_id, page_url FROM page WHERE name = "Buenos Aires, Argentina" AND type = "CITY"
The type column equals CITY is important, as otherwise other non-relevant pages URLs may be returned.
Anyhow, in order to get all events by a user and display their rsvp status, display information about the venue and also get the geographic places page URL I'm running the following mult-query FQL:
[events_query] => SELECT eid, name, pic_square, start_time, end_time, location, description, all_members_count, venue FROM event WHERE eid IN (SELECT eid FROM event_member WHERE uid = 6747251459 AND start_time > 0) AND start_time > 0 AND end_time < now() ORDER BY start_time desc LIMIT 5
[rsvp_query] => SELECT eid, rsvp_status FROM event_member WHERE eid IN (SELECT eid FROM #events_query) AND uid = YOUR_EVENTS_UID
[venue_page_id_query] => SELECT page_id FROM place WHERE page_id IN (SELECT venue.id FROM #events_query)
[venue_info_query] => SELECT page_id, name, page_url, location FROM page WHERE page_id IN (SELECT page_id FROM #venue_page_id_query)
[geo_city_country_query] => SELECT name, page_url FROM page WHERE name IN (SELECT concat(location.city, ', ', location.country) FROM #venue_info_query) AND type = "CITY"
My problem is that when doing the "geo_city_country_query" the response slows down quite a bit. To approx +5seconds. Without the "geo_city_country_query" query, the response time is approx 1.5sec-2sec.
Is there any way I can get this done faster?
Thanks in advance!

Facebook API - FQL - How to read places where my friends were tagged

How to read places where my friends were tagged ?
I found sth like this, but this example is not working for me .
or do I have to check piece by piece
SELECT page_id, author_uid FROM location_post WHERE xxfriendIDxx IN tagged_uids ??
{
"my_friends":"SELECT uid, name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me() ORDER BY rand() LIMIT 100)",
"their_locations":"SELECT page_id, author_uid FROM location_post WHERE tagged_uids IN (SELECT uid FROM #my_friends) LIMIT 100",
"those_places": "SELECT page_id, name, location FROM page WHERE page_id IN (SELECT page_id FROM #their_locations)"
}
Thx for help!
The below query will give you all the places that your friends have been tagged.
SELECT page_id, name, location FROM page WHERE page_id IN (SELECT page_id FROM location_post
WHERE author_uid IN
(SELECT uid2 FROM friend WHERE uid1=me()))
Make sure you have enabled the friends_photos, friends_checkins and friends_status permissions.
I couldn't get any data either until I changed your their_locations sub-query to this:
SELECT page_id, author_uid FROM location_post WHERE
tagged_uids IN (SELECT uid FROM #my_friends)
OR author_uid IN (SELECT uid FROM #my_friends)
LIMIT 100
It still isn't returning some location tagged posts that I can see using /FRIEND_USERNAME/locations.
There are other friends I know who post location tagged items all the time, but their privacy settings must be inhibiting these from being returned by the API.
You need the user_tagged_places permission. API V-4
<?php
//permissions array
echo '<li> Login With Facebook</li>';
//call field
echo '<pre>' . echo $graphObject['tagged_places'] . '</pre>';
?>