Get mutual friends from Facebook friends (MySQL table) - php

I'm trying to get mutual friends between 2 people.
I've saved the persons friends in a table called "friends" with the following fields:
id | facebook_id | name | uid | timestamp
id = unique id for the record
facebook_id = the friends facebook id
name = the friends name
uid = the users uid on my site, which is a friend to the person saved in the table
timestamp = don't need to explain :-)
Hope it make sense, have tried various ways to get the friends, but without luck

I dont know if using subqueries would be faster or mySQL already optimizes it.
Solution with subqueries:
SELECT f1.id, f1.name
FROM (SELECT id, name FROM friends WHERE uid=1) f1
JOIN (SELECT id, name FROM friends WHERE uid=2) f2
ON f1.id=f2.id;

It would be nice if MySQL has an INTERSECT operator, but...
You can grab all the friends of one person:
SELECT id, name
FROM friends
WHERE uid = 1
You could then JOIN this list back to the friends table, looking for the same friend for the other user:
SELECT f1.id, f1.name
FROM friends f1
JOIN friends f2 on f1.id = f2.id
WHERE f1.uid = 1
and f2.uid = 2
It's pseudo-code, but it should be close.

Related

PHP MySQL Search for users by mutual friends and country

I am developing a sort of social network of sorts. Today I started making a dynamic search bar with AJAX and php. In in I have a query that searches for users in a "users" table. I would like to order my search results by relevance, if you know what I mean. Like for instance if I have user with id 3, searching for John, I would like to order the results by the number of mutual friends, and possibly where the person is living.
My tables look like this
Users
id| name | country
Friends
id|user_id|friend_id
the queries I have for the moment are
SELECT * FROM users WHERE name LIKE $keyword
for user searching
and
SELECT COUNT(*) as mutual from users where id in
(select friend_id from friends where user_id = 3 and friend_id in
(select friend_id from friends where user_id = 7)
This for instance will return the number of mutual friends between user 3 and user 7.
How can I combine the two, in order to give more relevant search results?
Thank you in advance!
Like for instance if I have user with id 3, searching for John, I
would like to order the results by the number of mutual friends
Find friends of my friends whos name contains 'john' order by number of mutual friends:
select u.*, count(f1.user_id) as num_mutual_friends
from friends f1 -- my friend
join friends f2 on f2.user_id = f1.friend_id -- friend of my friend
join users u on u.id = f2.friend_id
where f1.user_id = 3
and u.name like '%john%'
and u.id <> 3
group by u.id
order by num_mutual_friends desc;
Show users from US first:
order by (u.country = 'us') desc, num_mutual_friends desc;
http://sqlfiddle.com/#!9/728c8a/3

Follow friend of friends suggestion MySQL

I'm creating a 'Follow Suggestion' feature, where I'd like to show random Friends of my Friends who's NOT my friend.
user_friends table
friend_id | friend_one | friend_two | role
user table
uiD | username
For each friend that a user follow it makes two records. If user 1 and 2 become friends a record would be created where, friend_one = 1 & friend_two = 2, friend_id = ramdom AUTO_INCREMENT number role = fri in the user_friends table.
User table, just has the users id and username.
How would I make a sql query that suggests "who to follow" based on friends of my friends ? Just how twitter does it.
What I've tried hasn't quite worked which does not seem to make much sense even to me.
http://pastebin.com/tCt6jdAZ <- Query code. ( Don't want to post here because feels useless ).
If I understand your question correct:
you need to join your 'user_friends' table twice:
select * from
user_friends level1,
user_friends level2
where
level1.friend_two = level2.friend_one -- or opposite i am not sure if a understood your model
and level1.friend_one = 'starting friend id'

php friend system, display friends of my friends

I have a table named friends, keeps my friends in a php friend system. The table is as follows:
friend_id | user_one | user_two
My script is the following for displaying my friends.
$check_friend_query = mysql_query(" SELECT friends_id from friends WHERE (user_one='$session_user_id' AND user_two ='$user_id') OR (user_one='$user_id' AND user_two='$session_user_id') ");
if( mysql_num_rows($check_friend_query) == 1 ){
echo" 1st degree friend";
}
All I want is to display 2nd degree friends. My 2nd degree friends are the friends of my friends. Any idea how to do this?
You can run an original query to grab all of your friends (like you have), then another to find their friends using their user ID as the variable to look for.
mysql_query("SELECT friends_id from friends WHERE user_one='$user_id'");
Something like this would check friends_id for all friends where the name is the user_id of the person.
Although the variables are a bit difficult to figure out, since I don't know the script.
Join the friends against itself:
SELECT degree2.*
FROM friends
LEFT JOIN friends AS degree2 ON friends.user_two = degree2.user_one
WHERE friends.user_one = 'original friend';
For further degrees, you'd just add another join level, e.g.
LEFT JOIN friends AS degree3 ON degree2.user_two = degree3.user_one
LEFT JOIN friends AS degree4 on degree3.user_two = degree4.user_ond
etc...

Check If Some User ID's Are Friends With The Logged In User In A Single Query

I have a simple friends system on my site. Now I'm trying to create a multi-user group messaging system but only between friends. The "To" values of my messaging system are comma values, I want to check if they all are friend with the person sending the message.
For example, I can get all of a user's friends by the following query:
SELECT relation_setter user_id
FROM users_relationships
WHERE relation_getter = {$logged_in_user}
AND active = 1
UNION
SELECT relation_getter user_id
FROM users_relationships
WHERE relation_setter = {$logged_in_user}
AND active = 1
and I have natasha, reeta as a $_POST['to'] value from my message form which I then convert to user ids and get something like 126152, 112238
Now how do I check if these ids are both friends with the logged in user, in a single query. I don't want to run my is_friend function in a loop, which check if a single userid is friends with the logged in user.
Relationships Schema:
users_relationships
id PK
relation_setter FK user_id
relation_getter FK user_id
active smallint(1)
The relation_setter is the one who sent the friend request. To get all my friends I get all the IDs where my ID is either the relation_setter or relation_getter.
users
user_id PK
name
username
password
etc etc...
Your post offers vague insight into the schema, so I will use some assumptions
You can find all of the ids that match their friends via an IN statement. Since you already have them as numeric values with comma's you could do:
SELECT user_id
FROM users_relationships
WHERE relation_getter IN (126152,112238,123456)
AND active = 1
This will return ONLY the records of friends that match. You then could match the number of rows with the number of elements in the query to determine if they're friends or not. You could also just send to the ones that matched.
EDIT
SELECT user_id
FROM users_relationships
WHERE relation_getter IN (126152,112238,123456)
OR relation_setter IN (126152,112238,123456)
AND active = 1
This will return all user ID's of the person's friends be they the getter or setter and it's active.
EDIT 2
new table
relationships_members
id FK (from users_relationships; not unique)
user_id
Sample of relationships_members would be
id | user_id | relation_setter
--------------------------------------
1 12345 1
--------------------------------------
1 98765 0
--------------------------------------
Then if you queried, you would only receive users_relationships ID that were valid
select distinct a.id, b.user_id as friend
from (
select distinct id as friend
from relationships_members
where user_id = {$logged_in_user}
) a, relationships_members b
WHERE a.id = b.id
and user_id IN (126152,112238,123456)

PHP: Search in one table and in another

I am making an autosuggesting function, when the user writes something in the field it stores it in:
$queryString = $db->real_escape_string($_POST['queryString']);
I want it to autosuggest after the users friends. The user´s friends is in users_friends, but only the friend´s ID. Now their full_name is in the table "users". And i want when you search it should in users for the full_name + check if its friends with the user.
As you may understand i do not expect all my users to know eachother id´s so writing e.g "52" "233", but searching for their full_name s.
UPDATE:
I know tried doing this:
$query = $db->query("SELECT uf.bID
FROM users friends, users_friends uf
WHERE uf.uID = '1' AND uf.type = 'friend' AND friends.full_name LIKE '$queryString%' LIMIT 10;"
);
It selects the bID, from the users friends WHERE the userid is 1 and are friend.
Now i start to see some results i think. When i write a full_name that im friends with, i get the id of the user(the id that is stored in bID). Now i just need to grab the full_name in "users" where id = bID..
table: users
id | full_name
table: users_friends
id | uID | bID
So conclusion of all this (trying to make a better summary in order to make you understand better: )
When you type in e.g Jack in the search field, then the $queryString is now "jack". Then it is taking "Jack"(full_name in users), grabbing his id(id in users), if he exists there ofcourse, and then match it with bID (in users_friends) where uID is $USER; ($user is the current user that are logged in´s id.)
Hope this was easier to understand, please leave comment if theres something unclear.
So, as i figure it out, you've got the current user's id in $USER and its query string in $queryString, and what you want is the names of the user's friends based on the $queryString, am I right?
So, assuming the database's schema is as you've put:
table: users
id | full_name
table: users_friends
id | uID | bID
See if this query works out for you, then:
SELECT users.full_name
FROM users INNER JOIN users_friends ON users.id=users_friends.uID
WHERE bID=$USER AND users.full_name LIKE '$queryString%'
LIMIT 10;
Where $USER and $queryString are your variables.
Do you want to read data from many tables at once??
SELECT table1.id, table2.name FROM table1, table2 WHERE ...
My english is not very good to understand everything :D
I didn't understood your question
But you can use this query:
mysql_query("SELECT * FROM table_one, table_two WHERE table_one.id = table_two.id");

Categories