Friend suggestion MySQL query
I have a users table with each member having an unique iD. e.g.
uiD | username | photo
Second table is user_friends table where each has an unique iD as well.
friend_id | friend_one | friend_two | role
I'm starting to learn MySQL join, AS and such but I'm confused how to map
my query due to how my user friends follow system is printed out. If an
user registers the following information is inserted in MySQL databse.
user table
150 | StackOverFlow | ...
160 | user2 | ...
user_friends
20 | 150 | 150 | me
25 | 160 | 160 | me
Now If I head to an user profile and click the follow button, then the
following happens. e.g. Let's say I'm StackOverFlow iD = 150
user_friends
150 | 150 | 160 | fri
StackOverFlow(150) is now following user2(160).
I'm unsure of how to map my query due to this reason, I'm not great at
MySQL. Any help would be appreciated as I'm stuck.
Here is what I've got so far.. which I found some leads on another thread.
SELECT
me.id AS member_id,
their_friends.friend_id AS suggested_friend_id,
COUNT(*) AS friends_in_common
FROM
user AS me
INNER JOIN
user_friends AS my_friends
ON my_friends.uiD = me.id
INNER JOIN
friends AS their_friends
ON their_friends.uiD = my_friends.friend_id
LEFT JOIN
friends AS friends_with_me
ON friends_with_me.uiD = their_friends.friend_id
AND friends_with_me.friend_id = me.id
WHERE
friends_with_me.uiD IS NULL
GROUP BY
me.id,
their_friends.friend_id;
What I'm trying to accomplish is pretty much this..
Photo | Name | Follow Button |
No comments:
Post a Comment