Get all the friends of friends list - mysql

I have a table "friendship" like this
user_id
friend_id
For each friendship I make one record instead of two.
---------------------
user_id | friend_id |
--------------------
1 | 2 |
--------------------
And I Don't add (2 , 1) into table.
So, I need to get all the friends of friends list including those who are already in my friend list preferably without subqueries. Any suggestions ?
Thanks :)

Are you looking for something like this?
SELECT f2.friend_id
FROM friendship f1 JOIN friendship f2
ON f1.friend_id = f2.user_id
WHERE f1.user_id = 1
Here is SQLFiddle demo

Related

get friends of friend using mysql

I am making a query in which i want to get friends of friends but the problem is that if my friends are also friends it suggesting their ids too . Suppose user id is 1 and he has teo friends 2 and 3 and 2 has is friends with 4 and 3 and 3 is friends with 5 and 2 then its suggesting me 4,5 which it should but also suggesting 2,3 because they are friends and they both are also my friends which is wrong it should only suggest 4,5
My query is
SELECT fr.friend_id
FROM friend_list fl ,friend_list fr
WHERE fl.login_id='27'
AND fl.status='3'
AND fl.friend_id=fr.login_id
AND fr.status='3'
AND fl.login_id!=fr.friend_id
One way to do this is to add WHERE NOT IN predicate :
SELECT fr.friend_id
FROM friend_list fl
INNER JOIN friend_list fr ON fl.friend_id = fr.login_id
AND fl.status = fr.status
WHERE fl.login_id = 27 AND fl.status = '3'
AND fr.friend_id NOT IN(SELECT friend_id
FROM friend_list
WHERE login_id = 27 AND friend_id IS NOT NULL)
The subquery after the NOT IN will select the friends of the friend id you are passing. to exclude them.
SQL Fiddle Demo
This will give you only 4, 5 for the example you gave in your question:
| FRIEND_ID |
|-----------|
| 4 |
| 5 |
Note that: I used the explicit JOIN syntax instead of the old JOIN syntax you are using, it should be the same but it is recommended to use the new one.

How to put logic into SQL?

First at all i am sorry for asking silly question like this.
I am new into sql. Leaned basic things from internet but cant figure this out.
Lets think i have a table named post where i store peoples post. Table structure is like this..
Post_id | poster_id | text
1 | 12 | "hello this is a post"
2 | 15 | "Another post"
3 | 77 | "More counting"
There is a table named "Friends" Where i store whos friend is who. Table structure is like this..
user_one | user_two
1 | 88
84 | 33
1 | 66
Now i can use SQL like this to get post from post
SELECT * FROM POST INNER JOIN frnends ON user_one = poster_id WHERE 1
But this will get a table by joining two table together, Cant i do some logic like this?
SELECT post FROM post WHERE poster_id = (SELECT user_two,user_one FROM friends WHERE user_one = MY_id OR user_two = MY_id
perhaps MY_id is a variable WHERE MY_id = 1
Now to do this kind of logic?
Any help will be so great.
Sorry, I lack the reputation to comment. Gamal's solution should be refined to prevent the return of My_id's posts;
SELECT *
FROM post p
INNER JOIN friends AS f on p.poster_id IN (f.user_one, f.user_two)
WHERE My_id IN (f.user_one, f.user_two)
AND p.poster<>My_id;
Or if you do want those posts as well;
SELECT *
FROM post p
INNER JOIN friends AS f on p.poster_id IN (f.user_one, f.user_two)
WHERE My_id IN (f.user_one, f.user_two)
AND p.poster<>My_id
UNION
SELECT *, My_id AS user_one, NULL AS user_two
FROM post p
WHERE p.poster=My_id;
The problem is that Gamals suggestion will return each post by My_id once for every relation that My_id is part of.
try this:
SELECT *
FROM [sample].[dbo].[posts] a inner join [sample].[dbo].[friends1] b on a.posterId=b.user_one or a.posterId=b.user_two where a.PosterId=12

Friends my Friends recently add

Im building a platform where users can connect with other users (Social platform)
I have a Table called friends and i am saving the connections like this
user_id | friend_id | request | add_date
Now i need to write a sql query to get the most recent friends a specific user's friends added that they are or not already friends of the user. Also the user must be accepted.
Think of it as a news feed and i was to see who my friends recently added (the new recently add person can be my friend or not)
So far i have this but works only when my friend added people i already have.
SELECT user_main_id AS frmname, friend_id AS type_id, add_date AS date
FROM friends
WHERE friend_id
IN
(SELECT friend_id
FROM friends WHERE (friend_id='$user_id' OR user_main_id='$user_id')
AND request=1 AND friend_id!=$user_id)
AND request=1 AND friend_id!=$user_id AND user_main_id!=$user_id
ORDER BY date DESC
Maybe there is a better why to approach this.
Suggestions? Much appreciated thanks. The connection is bilateral, no difference between user_id and friend_id. Was designed with those names and had to be carried forward.
Sample Record
96618 50683 1 2013-05-08 13:44:31
96618 1230 1 2013-04-03 18:28:51
11671 96618 1 2013-04-03 13:26:51
11671 1230 1 2013-03-23 18:26:08
Once 96618 connects with 50683 happens. users 11671 for example will get a msg saying your friend 96618 is now friends with 50683
Try this:
SELECT f2.friend_id
FROM friends f1 # Friends of target user
INNER JOIN friends f2
ON f1.friend_id = f2.user_id # Friends of their friends
AND f2.request = 1
AND f2.friend_id != f1.user_id
INNER JOIN friends f3 # Limiting it to mutual friends
ON f2.friend_id = f3.user_id AND f3.friend_id = f1.user_id
WHERE f1.user_id = 11671 AND f1.request = 1
ORDER BY f2.add_date DESC
Please note that with your current table structure, you are going to need two rows for each friendship, one for each direction. To do it with only one row, you probably want to split it into two tables - friendships and friendship_members.

MySQL Query to find friends and number of mutual friends

I have looked through the questions but I cant find anything that does exactly what I need and I can't figure out how to do it myself.
I have 2 tables, a user table and a friend link table. The user table is a table of all my users:
+---------+------------+---------+---------------+
| user_id | first_name | surname | email |
+---------+------------+---------+---------------+
1 joe bloggs joe#test.com
2 bill bloggs bill#test.com
3 john bloggs john#test.com
4 karl bloggs karl#test.com
My friend links table then shows all relationships between the users, for example:
+--------=+---------+-----------+--------+
| link_id | user_id | friend_id | status |
+---------+---------+-----------+--------+
1 1 3 a
2 3 1 a
3 4 3 a
4 3 4 a
5 2 3 a
6 3 2 a
As a note the a in the status column means approved, there could also be r(request) and d(declined).
What I want to do is have a query where if a user does a search it will bring back a list of users that they are currently not already friends with and how many mutual friends each user has with them.
I have managed to get a query for all users that are currently not friends with them. So if the user doing the search had the user id of 1:
SELECT u.user_id,u.first_name,u.surname
FROM users u
LEFT JOIN friend_links fl
ON u.user_id = fl.user_id AND 1 IN (fl.friend_id)
WHERE fl.friend_id IS NULL
AND u.user_id != 1
AND surname LIKE 'bloggs'
How then do I have a count of the number of mutual friends for each returned user?
EDIT:
Just as an edit as I don't think I am being particularly clear with my question.
The query that I currently have above will produce the following set of results:
+---------+------------+---------+
| user_id | first_name | surname |
+---------+------------+---------+
2 bill bloggs
4 karl bloggs
Those are the users matching the surname bloggs that are not currently friends with joe bloggs (user id 1).
Then I want to have how many mutual friends each of these users has with the user doing the search so the returned results would look like:
+---------+------------+---------+--------+
| user_id | first_name | surname | mutual |
+---------+------------+---------+--------+
2 bill bloggs 1
4 karl bloggs 1
Each of these returned users has 1 mutual friend as joe bloggs (user id 1) is friends with john bloggs and john bloggs is friends with both returned users.
I hope this is a bit more clear.
Thanks.
Mutual friends can be found by joining the friend_links table to itself on the friend_id field like so:
SELECT *
FROM friend_links f1 INNER JOIN friend_links f2
ON f1.friend_id = f2.friend_id
WHERE f1.user_id = $person1
AND f2.user_id = $person2
But bear in mind that this, in its worst case, is essentially squaring the number of rows in the friend_links table and can pretty easily jack up your server once you have a non-trivial number of rows. A better option would be to use 2 sub-queries for each user and then join the results of those.
SELECT *
FROM (
SELECT *
FROM friend_links
WHERE user_id = $person1
) p1 INNER JOIN (
SELECT *
FROM friend_links
WHERE user_id = $person1
) p2
ON p1.friend_id = p2.friend_id
Also, you can simplify your friend_links table by removing the surrogate key link_id and just making (user_id,friend_id) the primary key since they must be unique anyway.
Edit:
How would this be applied to the original query of searching for users that aren't already friends, I would like to do both in a single query if possible?
SELECT f2.user_id, COUNT(*) 'friends_in_common'
FROM friend_links f1 LEFT JOIN friend_links f2
ON f1.friend_id = f2.friend_id
WHERE f1.user_id = $person
GROUP BY f2.user_id
ORDER BY friends_in_common DESC
LIMIT $number
I am also thinking that the user_id constraints can be moved from the WHERE clause into the JOIN conditions to reduce the size of the data set created by the self-join and preclude the use of subqueries like in my second example.
This query lists anyone who's not friend with user 1 and whose surname matches '%bloggs%':
SELECT
users.user_id,
users.first_name,
users.surname,
Sum(IF(users.user_id = friend_links_1.friend_id, 1, 0)) As mutual
FROM
users inner join
(friend_links INNER JOIN friend_links friend_links_1
ON friend_links.friend_id = friend_links_1.user_id)
ON friend_links.user_id=1 AND users.user_id<>1
WHERE
users.surname LIKE '%bloggs%'
GROUP BY
users.user_id, users.first_name, users.surname
HAVING
Sum(IF(users.user_id = friend_links.friend_id, 1, 0))=0
just change the user id on the ON clause, and the surname on the WHERE clause. I think it should work correctly now!
If A is friend of B, then B is also a friend of A? Wouldn't it be better to use just a link instead of two links (and instead of two rows in friends_links)? Then you have to use two status columns, status1 and status2, and A is friend of B only if status1 = status2 = "a".
There are many ways to show mutual friends, e.g.:
SELECT friend_id
FROM friend_links
WHERE friend_links.user_id = $user1 or friend_links.user_id = $user2
AND NOT (friend_links.friend_id = $user1 or friend_links.friend_id = $user2)
GROUP BY friend_id
HAVING Count(*)>1
And this query shows for each user and anyone who's not his/her friend:
SELECT
users.user_id,
users.first_name,
users_1.user_id,
users_1.first_name
FROM
users INNER JOIN users users_1 ON users.user_id <> users_1.user_id
WHERE
NOT EXISTS (SELECT *
FROM friend_links
WHERE
friend_links.user_id = users.user_id
AND friend_links.friend_id = users_1.user_id)
(The only think I didn't check is the friendship status, but it's easy to add that check).
I'm still working on it, but it's not easy to combine nicely these two queries togheter. So this isn't exactly an answer, I'm just showing some ideas that i've tried.
But what do you need exactly? A query that returns every user with anyone who's not his/her friend and the number of friends in common, or is the user_id already given?
With some code it's not a problem to answer your question... but there has to be a nice way just by using SQL! :)
EDIT:
I'm still wondering if there's a better solution to this, in particular the next query could be extremely slow, but it looks like this might work:
SELECT
users_1.user_id,
users_2.user_id,
Sum(IF(users_1.user_id = friend_links.user_id AND users_2.user_id = friend_links_1.friend_id, 1, 0)) As CommonFriend
FROM
users users_1 INNER JOIN users users_2
ON users_1.user_id <> users_2.user_id,
(friend_links INNER JOIN friend_links friend_links_1
ON friend_links.friend_id = friend_links_1.user_id)
GROUP BY
users_1.user_id,
users_2.user_id
HAVING
Sum(IF(users_1.user_id = friend_links.user_id AND users_2.user_id = friend_links.friend_id, 1, 0))=0
(as before, i didn't check friendship status)
If user is given, you could put WHERE users_1.user_id=$user1 but it's better to just leave one user table, and filter the next INNER JOIN whith that user.

"Friend" relationships across two mysql tables

I'm not quite sure how to construct an sql join inorder to find who a specific users "friends" are.
For example I have two table
User Table
u_ID | u_Name
-------------
1 | bob
2 | jill
3 | jack
4 | susan
Friends Table
f_ID | u_ID1 | u_ID2
--------------------
1 | 1 | 2
2 | 3 | 1
3 | 4 | 2
I need to find a way of getting all of bobs friends, or all of jills friends, for example.
Friends cannot have duplicate results
I.e. can have a row with either (u_ID1 = 1, u_ID2 = 2) or (u_ID1 = 2, u_ID = 1) but not both, as they are technically the same.
Here is my incorrect query
SELECT u.u_Name
FROM user u
INNER JOIN friends f ON (f.u_ID1 = '1' OR f.u_ID2 = '1')
Thanks in advance
Solution
Check Kris Babic reply for solution,
also thank you for everyone elses help
This uses a STRAIGHT join, but should work:
select u.u_Name
from friends f, user u
where (f.u_ID1 = '1' and u.u_ID = f.u_ID2)
or (f.u_ID2 = '1' and u.u_ID = f.u_ID1)
For all of bob's friends this should work (untested)
select u.u_Name
FROM user u
inner join friends f1 on f1.u_uID1 = u.u_ID
inner join friends f2 on f2.u_uID2 = u.u_ID
where u.u_ID = 1
Try this:
SELECT u1.u_Name as user1 , u2.u_Name as user2
FROM user as u1 INNER JOIN friends ON u1.u_ID=friends.u_ID1
INNER JOIN user as u2 ON u2.u_ID=friends.u_ID2