Talk:Sample FQL Queries

From Facebook Developer Wiki

Revision as of 02:33, 22 August 2009 by Richard Lynch (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Contents

[edit] Query for friends who've added the current app

Is this the best why to query for a list of the uid's of all my friends who have the current application installed?

 SELECT uid FROM user WHERE uid IN (
    SELECT uid2 FROM friend WHERE uid1=12933936
        ) AND has_added_app=1

Thanks --Tony 06:23, 13 June 2008 (PDT)


As far as I can tell this just tells you that you can't look up all friends of user such and such. - Adamtk

[edit] Aggregate Queries

Is there any way of doing an aggregate query in FQL? For example, if I wanted the sexes of all of the user's friends in MySQL you would do:

SELECT sex, COUNT(sex) AS sex_count FROM user WHERE uid IN (COMMA_SEPERATED_UIDS)

Which would return:

sex | sex_count

male | 334 female | 123 other | 392


Something like that. Why does Facebook deny COUNT, SUM, MAX functionality in FQL?


[edit] You could do

 try {
 $query = "SELECT sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = xxxx  ) AND sex = 'Male'";
 $rows = facebook_client()->api_client->fql_query($query);
 echo "There are ".count($rows)." Males";
 }
 catch (Exception $e) {
 error_log("Failure in the api: ". $e->getMessage());
 }

Benjamin --613286 09:21, 23 December 2008 (PST)


[edit] Finding a Date in Advance

SELECT uid FROM user WHERE strpos(birthday, "June") = 0 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $app_user) SELECT uid FROM user WHERE (strpos(birthday, "$today") = 0 AND strlen(birthday) = $todaylength OR strpos(birthday, "$today,") = 0 ) AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $app_user


These functions are great but how you I look up all birthdays that come within 7 days? Is there a way to replace "$today" with "<$today+7"?

Hi Jeremy, you'd have to run your query with a series of OR clauses, one for each date you're looking for -- since you're already defining $today elsewhere, you can keep changing the value of the date in your WHERE clause. Also, it's a good idea to use the birthday_date column from the user (FQL) table, since that format never changes. The birthday field may be formatted according to the user's locale, so you might get results like "21 April" or "April 21", whereas birthday_date will always return "4/21". Pete (563683308 13:34, 12 June 2009 (PDT))

[edit] LIMIT 1-based? 0-based?

It's not actually documented, but I'm guessing 1-based from the 1,42 example... --1190307405 19:33, 21 August 2009 (PDT)

reference