I would like to query user information like (first_name, last_name, pic, uid) then join with comments data such as text, time.
first this is my query for comment
SELECT text, time, username, fromid FROM comment WHERE post_id ='123456'
then i need to join this query with user
SELECT first_name, last_name FROM user WHERE **uid='fromid'**
uid from user table can join with fromid from comment table.
how can i do it?
thank
FQL doesn't support joins:
Unlike SQL, the FQL FROM clause can contain only a single table. You can use the IN keyword in SELECT or WHERE clauses to do subqueries, but the subqueries cannot reference variables in the outer query's scope.
Your best bet would be probably to get all user ids from comment results, compile them into a list and run
SELECT first_name, last_name FROM user WHERE uid in (1,2,3,4,5)
And then manually join users with comments.
If you don't want to compile list of userids manually you can run a slightly more heavy query to get all users from comments:
SELECT first_name, last_name FROM user WHERE uid in (SELECT fromid FROM comment WHERE post_id ='123456')
but you still would need to join users and comments manually.
Related
I have a table with users and a table with posts. (users) one to many (posts) and posts has the column user_id referenced to users.id. How to query all users and return all users with count of posts for each users?
ps: I'm also using knex.js query builder
SELECT users.id,count(posts.id) FROM users JOIN posts ON users.id=posts.user_id GROUP BY users.id
knex.select('users.id', knex.raw('count(posts.id)')).from('users').join('posts', 'users.id', '=', 'posts.user_id').groupByRaw('users.id')
PS : use LEFT JOIN rather than JOIN to see also users with no post
I can get events created by my friends:
SELECT eid, name, start_time, pic_cover.source, location, venue FROM event
WHERE creator IN (SELECT uid2 FROM friend WHERE uid1=me())
But I want to get my friends names asociated to that events. How can I think of that?
As FQL doesn't support joins between tables, the only solution I can think of is that you match the names of your friends and the events they created in your application.
Therefore, you'd need to issue two separate statements:
SELECT uid, first_name, last_name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=me())
and
SELECT eid, creator, name, start_time, pic_cover.source, location, venue FROM event WHERE creator IN (SELECT uid2 FROM friend WHERE uid1=me())
and then match them via uid (from the first query) and creator (from the second query).
I can get the number of likes on a status through this FQL query -
SELECT like_info,
message,
status_id,
time,
uid
FROM status
WHERE uid = me()
I'd like to get the separate user ID's of the likes (if not possible for friends of the authenticated user, just the user himself/herself). It's possible through the Graph API through a command like this -
me/?fields=statuses
Is the same thing possible through FQL?
You can select IDs from status table for user and then use them to select data from like table
SELECT user_id,
object_id
FROM like
WHERE object_id IN(
SELECT status_id
FROM status
WHERE uid = me() )
You may group result by object_id on your side.
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
I would like to see how I was answering questions, asked in some Facebook group I am a member of. Is there a way to do it with the API?
Follow the steps:
First:
your application need permission to user_questions and user_groups
Second:
you can use a multi-query to get all yours questions, answer and voters with basic information.
Third:
Use https://developers.facebook.com/tools/explorer to check the data
And the end, use this query:
fql?q={
"questions":"select id,question from question where owner= me()",
"options":"select id, question_id, name,votes from question_option WHERE question_id in (select id from #questions)",
"option_votes":"select voter_id, option_id from question_option_votes where option_id IN (select id from #options) order by voter_id desc",
"users":"select uid, name, first_name, middle_name, last_name, sex, locale, pic_small_with_logo, pic_big_with_logo, pic_square_with_logo, pic_with_logo, username from user where uid IN (select voter_id from #option_votes)"
}
Note: the fields "uid, name, first_name, middle_name, last_name, sex, locale, pic_small_with_logo, pic_big_with_logo, pic_square_with_logo, pic_with_logo, username" is public access and don't need a token
You can query the question_option_votes fql table.
However you need to have the appropriate permissions to do so:
To read options from the question_option table you need the following permissions:
user_questions for questions posed by the current user.
friends_questions for questions posed by friends of the current user.
I doubt that, unless your an admin of that group, you would not be able to get that data.