mysql - Friends my Friends recently add -
im building platform users can connect other users (social platform)
i have table called friends , saving connections
user_id | friend_id | request | add_date
now need write sql query recent friends specific user's friends added or not friends of user. user must accepted.
think of news feed , see friends added (the new add person can friend or not)
so far have works when friend added people have.
select user_main_id frmname, friend_id type_id, add_date date friends friend_id in (select friend_id friends (friend_id='$user_id' or user_main_id='$user_id') , request=1 , friend_id!=$user_id) , request=1 , friend_id!=$user_id , user_main_id!=$user_id order date desc
maybe there better why approach this.
suggestions? appreciated thanks. connection bilateral, no difference between user_id , friend_id. designed names , had 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 50683 happens. users 11671 example msg saying friend 96618 friends 50683
try this:
select f2.friend_id friends f1 # friends of target user inner join friends f2 on f1.friend_id = f2.user_id # friends of friends , f2.request = 1 , f2.friend_id != f1.user_id inner join friends f3 # limiting mutual friends on f2.friend_id = f3.user_id , f3.friend_id = f1.user_id f1.user_id = 11671 , f1.request = 1 order f2.add_date desc
please note current table structure, going need 2 rows each friendship, 1 each direction. 1 row, want split 2 tables - friendships , friendship_members.
Comments
Post a Comment