6

データベースに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 ) )


       ")
        }
4

4 に答える 4

1

最初のクエリは次のように書くこともできます。

select distinct u.id,
        u.username,
        u.avatar
    from users_table u where u.id in 
        (select case when u1_id=$profile_id then u2_id else u1_id end 
        from friends_table f where case when u1_id=$profile_id 
        then u1_id else u2_id end =$profile_id);

相互の友人のクエリは、同様の方法で単一のクエリとして記述できます。

select u.id, (select count(f.id) from friends f where 
    case when f.u1_id=u.id then u2_id else u1_id end in 
        (select distinct case when u1_id=$profile_id then u2_id else u1_id end 
        from friends where case when u1_id=$profile_id then u1_id else u2_id 
        end =$profile_id) 
    and u1_id=u.id or u2_id=u.id and 
    (u1_id <> $profile_id and u2_id <> $profile_id)) 
as mutual_frnds from user u where u.id <> $profile_id;

ただし、使用する前にいずれかのパフォーマンス テストを行うことをお勧めします。

于 2012-05-20T08:44:55.743 に答える
1

必要なのは 1 つのクエリだけです。

select id, username, avatar, -- ...
(
  select count(*)
  from friends_table f1
  inner join friends_table f2 on f1.u2_id = f2.u1_id and f2.u2_id = f1.u1_id
  where f1.u1_id = users_table.id
)
as mutual_friend_count
from users_table

サブクエリの意味は次のとおりです。

最初の友達関係のターゲットが 2 番目の友達関係のソースであり、2 番目の友達関係のターゲットが最初の友達関係のソースであるような、ユーザーが参加している「友達の友達」関係の数を教えてください。 1。

于 2012-05-22T22:05:24.377 に答える
0

テーブルへのフレンド関係ごとに 2 つの行を追加することにしました。

id    u1_id      u2_id  
1         10         20  
2         20         10

プロセスがより簡単かつ迅速になります。

于 2012-05-24T14:48:55.420 に答える
0

まず、ユーザーの友達を取得するための複雑なクエリがなぜそれほど複雑なのかわかりません...次のクエリで簡単に実現できます。

select u.id,
    u.username,
    u.avatar
from friends_table f
left join users_table u on f.u2_id = u.id
where f.u1_id = $profile_id

説明: ログインしているユーザーは、ID が と同じユーザーですf.u1_id。したがって、ID が にある友達のみを選択しますf.u2_id

次に、私の友達の共通の友達を数えるには、次のようなクエリを使用できます。

select count(*) as mutual_count, f.u1_id as mutual_friend_id
from friends_table f
where f.u1_id IN (select f.u2_id from friends_table where f.u1_id = {$profile_id})

$profile_id は、ログインしているユーザーの ID です...

これは正しいです?

于 2012-05-17T10:49:28.070 に答える