Facebook's fql improper ORDER BY sorting - facebook

I have this fql query:
SELECT link_id, owner, owner_comment, created_time, title, summary, url, image_urls
FROM link
WHERE owner IN (SELECT uid2
FROM friend
WHERE uid1 = me())
ORDER BY created_time DESC
LIMIT 0,200;
It's supposed to display last 200 posted links by my friends, but it displays them ordered by owner id ASC then by created_time DESC.
Is ORDER BY in fql limited to one use? How can I make it work?

I suspect the problem is the LIMIT statement. FQL has some odd internal behaviors to optimize their own internal API.
Try removing the LIMIT statement and just give it a specific AND created_time > <point in time> and see if it orders properly. If so, that's why.

Related

Facebook FQL getting all my friends last position update

I'm trying to get all my friend's last position update with this fql query:
SELECT author_uid, message, latitude, longitude, timestamp FROM location_post WHERE author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
I would like to take just the latest update for every friend but this query returns me more than one update for a single friend.
I tried to set a limit at the end of the query (equal to the number of my friends) but it does not work.
How I should change it?
If it was a normal database, not a facebook FQL, you could use join and get your results in single query. Unfortunately Facebook FQL does not allow joins or even max function.
But you can:
Receive all the friend ids like in your query
SELECT uid2 FROM friend WHERE uid1 = me()
Then for each friend uid run the query
SELECT author_uid, message, latitude, longitude, timestamp FROM location_post
WHERE author_uid = FRIEND_ID order by timestamp desc limit 1

fql OR and order by desc with offset

I have some issue with a Facebook's fql request :).
When I request like this
SELECT uid, profile_url
FROM user where contains('potatoe') OR uid in(select id from profile where contains ('potatoe'))
ORDER BY mutual_friend_count desc LIMIT 0,2
I have always the same results, even if I change the offset...
But
If I change DESC to ASC the "offset" works...
If I change OR to AND
and let the order by desc => the "offset" works too!
Something is wrong with my OR/desc/offset combo?
Or I just have misunderstood something with "or" operators? :/
Thanks a lot.
You should write your FQL query like that:
SELECT uid, profile_url
FROM user
where
uid in(select uid from user where contains ('potatoe') )
OR uid in(
select id from profile where contains ('potatoe')
)
ORDER BY mutual_friend_count desc
LIMIT 0,2
FQL couldn't interpret in boolean expression the result of "contains", so your OR operator failed here...
I think the problem is your query is asking the same question two different ways:
Show me all my friends user profiles that contain 'potatoe' OR
Show me all profiles that belong to users who are my friends that contain 'potatoe'.
Simplify your query to this and see if it works as expected.
SELECT uid, profile_url FROM user WHERE contains('potatoe')
ORDER BY mutual_friend_count DESC LIMIT 0,2

Friend Status FQL

I am attempting to pull the most recent 50 status updates by friends from Facebook using FQL, but it appears the status table nor the feed table work as expected. I am using the following query, but it's results are only the updates since the last Friday # midnight EST (i.e. at the time of this post 12/07/12 # 12AM EST) which is not enough for my need.
SELECT post_id,actor_id,target_id,message,created_time FROM stream WHERE source_id IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND filter_key = "nf" AND created_time > 1 AND type = 46 ORDER BY created_time DESC LIMIT 50
Is there another way to get the most recent 50 status updates from friends that I can use? I have tried the status table, but there are even less results in it.
From http://developers.facebook.com/docs/reference/fql/stream :
Each query of the stream table is limited to the previous 30 days or 50 posts, whichever is greater, however you can use time-specific fields such as created_time along with FQL operators (such as < or >) to retrieve a much greater range of posts.
Try the status table
SELECT message, status_id, uid FROM status WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY time DESC LIMIT 50
You can try this
SELECT message, status_id, uid FROM status WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me())
ORDER BY time DESC LIMIT 50
but be sure to have the friend permissons messege right ;)

Very complex FQL queries very slow

I'm trying to get the first 50 posts (posts by friends) from the user's news feed using facebook FQL, as well as each friend's profile information. I have two queries, which total 10 seconds of processing:
SELECT actor_id, message FROM stream
WHERE filter_key IN
(SELECT filter_key FROM stream_filter WHERE uid= me() AND type='newsfeed')
AND actor_id in (SELECT uid2 FROM friend WHERE uid1 = me())
LIMIT 50
This query returns the user, even if the message is "" (I couldn't find a NOT operator to filter out rows that have "" as a message)
Second query:
SELECT pic_big, name,url from profile WHERE id IN
(SELECT actor_id, message FROM stream WHERE filter_key IN
(SELECT filter_key FROM stream_filter WHERE uid= me()
AND type='newsfeed')
AND actor_id in (SELECT uid2 FROM friend WHERE uid1 = me()))
LIMIT 50
I've tried other ways to get the data (quicker) however, it returns it in a random order (facebook doesn't guarantee that the data comes out the way it went in).
I could place a "loading" animation on my website but I'd prefer not to because I think that this can be optimized but I'm not sure how.
In the past, I used the graph api but I had to process the results myself, and it was very inefficient (taking over 30+ seconds).
For these two queries, you should be able to speed things up quite a bit by combining them into a single multi-query, especially since your second query includes your first query. This one is the most complex, and if you're sending them in separate API calls, Facebook has to perform it twice.
{
'q1':"SELECT actor_id, message FROM stream
WHERE filter_key IN
(SELECT filter_key FROM stream_filter WHERE uid= me() AND type='newsfeed')
AND actor_id IN (SELECT uid2 FROM friend WHERE uid1 = me())
LIMIT 50",
'q2':'SELECT pic_big, name, url, id FROM profile WHERE id IN
(SELECT actor_id FROM #q1)'
}
Your two queries take 4072 + 1304 = 5376ms when I run them in the Graph API explorer. The multiquery takes 3475ms. With network latency eliminated from running two separate queries, this should drastically speed up your app.
You can filter out blank messages by adding AND strlen(message) > 0 to the WHERE part of your first query.
You can use ORDER BY column to force the return of data in a specific order, but there is no guarantee that you will get all the rows in your result.

How to I get all the checkins a user is tagged in using FQL

Using the graph api it's possible to do:
https://graph.facebook.com/me/checkins
But how am I supposed to do it with FQL?
I tried this which suggests:
SELECT message FROM checkin WHERE author_uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
AND me() IN tagged_uids
but it doesn't return even a single result.
Thanks.
From what I can see, IN doesn't seem to work with me(). You have to first get the user id and assign this to a variable. This throws an OAuth exception:
SELECT message FROM checkin WHERE author_uid = me() OR me() IN tagged_ids
but if you set [UserId] to the current user's id, this works:
SELECT message FROM checkin WHERE author_uid = [UserId] OR [UserId] IN tagged_ids
You don't need to use the friend subquery. FQL will only return checkins that are visible to your user.
For this to work, make sure your access_token has both the user_checkins and friends_checkins permissions.
#cpilko gave a solution, but I've found another one, so I am adding it here as a reference for whoever reach this page:
SELECT id FROM location_post WHERE ([UserId] IN tagged_uids OR author_uid=me()) AND strpos(type,"checkin")>=0