I have two tables in my database to maintain user information (users_table) and the other is tracking friends
users_table:
id username avatar 1 max max.jpg 2 jack jack.jpg
friends_table:
id u1_id u2_id 1 1 2 2 1 3
in each user profile I show a list of my friends
here is my request
select u.id, u.username, u.avatar from friends_table f join users_table u on f.u1_id = u.id || f.u2_id = u.id where u.id <> $profile_id and (f.u1_id = $profile_id || f.u2_id = $profile_id)
this request selects friends of the profile owner ($ profile_id)
and attach them to the user table to get each friend’s username and avatar
now I want to calculate mutual friends between each friend and the owner of the profile, is it possible in one request or do I need to make a long and probably slow request similar to this for each friend based (this is just an example and it may have some syntax error) :
foreach ( $friends_list_query_resul as $qr ){ $friend_id = $qr['id']; $mutual_count = mysql_query ( "select count(*) from friends_table where ($u1_id = $friend_id || $u2_id = $friend_id ) && ( $u1_id IN ( SELECT `u1_id`,`u2_id` from friends_table where ($u1_id = $profile_id || $u2_id = $profile_id ) ) || $u2_id IN ( SELECT `u1_id`,`u2_id` from friends_table where ($u1_id = $profile_id || $u2_id = $profile_id ) ) ") }
source share