Retrieving second level friendships from a table and excluding first level friends - mysql

I have the following table structure for a table Friendship:
| user varchar(255) | friend varchar(255) |
How can I retrieve a list of second level friends (i.e. John is friends with Adam who is friends with Steve, so Steve is a second level friend to John)?

SELECT f2.friend AS FriendOfFriend
FROM Friendship f
INNER JOIN Friendship f2 ON f.friend = f2.user
There's some detail missing from your question, so these joins may need to be adjusted. I'm assuming friends are also users.

Consider the following four rows of sample data:
| user | friend |
+------+--------+
| John | Sam |
| John | Adam |
| Adam | Steve |
| Steve | George |
In this example, John is friends with Adam, who is friends with Steve, so John and Steve are second level friends. To get that, we must first get a list of all John's friends, like this:
SELECT friend
FROM friendship
WHERE user = 'John';
Note that this makes the assumption 'John' is always the user. If John is sometimes 'friend', you'll need a union to get a complete list.
Now, we have a list of Adam and Sam. We need to get a list of their friends. We can do so by doing another join:
SELECT f.friend AS firstLevel, fs.friend AS secondLevel
FROM friendship f
JOIN friendship fs ON fs.user = f.friend AND f.user = 'John';
This will give us a list of John's second level friends, and who the first level friend is. Now, there are a few things to consider:
What if The same friend appears twice? Let's say, Sam is also friends with Steve. We will get the following rows:
| Adam | Steve |
| Sam | Steve |
To avoid that, we can just eliminate our select to the second level friend and use the DISTINCT keyword:
SELECT DISTINCT fs.friend
FROM friendship f
JOIN friendship fs ON fs.user = f.friend AND f.user = 'John';
If you ever need to expand this further, you can just add another join, and make sure it relates to the previous one. So, to get a third level connection, which in this example is George, we can use the following query:
SELECT DISTINCT fss.friend AS thirdLevel
FROM friendship f
JOIN friendship fs ON fs.user = f.friend AND f.user = 'John'
JOIN friendship fss ON fss.user = fs.friend;
All of the above examples are demonstrated on this SQL Fiddle.

I think this covers the issues I have raised to other answers.
SELECT DISTINCT a.user, b.friend
FROM Friendship a
JOIN Friendship b ON a.friend=b.user
WHERE a.user <> b.friend
AND NOT b.friend IN (SELECT c.friend FROM Friendship c WHERE a.user=c.user)
ORDER BY a.user
http://sqlfiddle.com/#!9/84091/1
The first part of the Where clause rules out someone being his own second level friend, the second part filters out second level friends who are also first level friends.

Related

Join 3 tables only if they have information on 1 table

I have 3 table
| Likes | | Friends | | Item |
----------------------------- ------------------ -------------------
| UserID | LikeID | itemID | | user1 | user2| | itemID |
I would like to join the 3 tables based on if the user has liked an item in the Likes table and the user must also be friends with the user in the Friends table and both of them have liked the item.
The way the Friends table is set up is if a user adds a friend the userID will be placed in either user1 or user2 depending who requested the friend first, so the userID of say 1 can be in either user1 column or user2 column and users friend will be in the opposing column.
Then I would do an inner join on itemID table to get the item both of the users liked.
I am no SQL expert so I am finding this very difficult and any code examples I give you would be rubbish. I have tried all types of left joins inner joins right joins I've joined the crap out of these tables but I do not get the correct data.
Any steer in the right direction on how to accomplish this would be great I am out of ideas. Please let me know of any further information I can give you.
I believe this does what you are after:
select a.user1, b.likeid, a.user2, c.likeid, d.itemid
from friends a
inner join likes b on a.user1 = b.userid
inner join likes c on a.user2 = c.userid
inner join item d on (b.itemid = d.itemid and c.itemid = d.itemid);
Here's an sqlfiddle demonstrating it: http://sqlfiddle.com/#!4/ff9d59/11

Mysql select between two table without limiting if record appear on the joined table

I have been trying to figure out how to select data related to one id between to tables without limit it to the joined table. I tried using UNION, Inner join, JOIN, but it limit me to show records that are only in both tables. By example:
Table 1 (users)
id | name | register
1 | John | 2014-03-01
2 | Kate | 2014-03-02
etc..
Table 2 (birthdays by example)
id | user | birthday
1 | 1 | 1989-09-09
Note that kate dont have a record on the birthdays table, if i do:
SELECT U.id, name, register, B.birthday FROM users as U INNER JOIN birthday as B ON B.user = U.id
it will only shows JOHN data, i would like to select all my users and if the record do not exist on the joined table, still be able to select all my users, sort of:
id | name | register | birthday
1 | John | 2014-03-01 | 1989-09-09
2 | kate | 2014-03-02 | null or ''
3
4
etc.
Sorry if its a stupid question but i dont find the light on this one. I would appreciate the help.
Regards
You need a LEFT OUTER JOIN instead of the plain JOIN (also known as INNER JOIN), like this:
SELECT U.id, name, register, B.birthday
FROM users as U
LEFT JOIN birthday as B
ON B.user = U.id
A LEFT JOIN between users and birthday tables will contain all records of the "left" table (users), even if the join-condition does not find any matching record in the "right" table (birthday).
This excellent article on The Code Project will help you a lot: Visual Representation of SQL Joins.
Summary of all JOIN types:
Note: Mysql does not support FULL OUTER JOIN but it can be emulated. Useful articles:
https://stackoverflow.com/a/4796911
http://www.sql-tutorial.ru/en/book_full_join_and_mysql.html
http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Use left outer join instead of inner join..
SELECT U.id, name, register, B.birthday
FROM users as U left join birthday as B ON B.user = U.id

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.

Get all the friends of friends list

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

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.