データベースに2つのテーブルがあります。1つはユーザー情報(users_table)を保持するためのもので、もう1つは友達を追跡するためのものです。
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
すべてのユーザープロファイルで、友達リストを表示します
これが私の質問です
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)
このクエリは、プロファイル所有者の友達を選択します($ profile_id)
それらをユーザーテーブルに結合して、各友達のユーザー名とアバターを取得します
今、私は各友人とプロファイル所有者の間の相互の友人を数えたいと思います1つのクエリでこれを行うことができますか、または設立された友人ごとにこのような長くておそらく遅いクエリを行う必要があります(これは単なる例であり、構文がある可能性がありますエラー ):
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 ) )
")
}