2

テーブル構造:

    users: uid, name
    friendships: uid1, uid2
    game_membership: gid, uid

基本的に、クエリを の関数として取得しようとしておりgid、次を返します。

uid1, name1, uid2, name2

uid指定された下のgame_membership表にある のみgidが返されます。これは私がこれまでに持っているものです:

SELECT 
   f.uid1 AS id1, 
   u1.name AS name1, 
   f.uid2 AS id2, 
   u2.name AS name2
FROM 
   friendships AS f
WHERE 
   u1.uid in (SELECT uid FROM game_membership WHERE gid = <GID>)
   AND u2.uid in (SELECT uid FROM game_membership WHERE gid = <GID>)
JOIN users AS u1 ON f.uid1 = u1.uid
JOIN users AS u2 ON f.uid2 = u2.uid

今、MySQL 構文エラーが発生しました。誰かが私を正しい方向に向けることができますか? テーブルuid内の で、それぞれに 1 つずつ、別の JOIN または 2 つを実行する必要があるのではないかと思います。game_membershipuid

4

4 に答える 4

9

where 句の代わりに Game_membership テーブルに参加することをお勧めします。

SELECT 
   f.uid1 AS id1, 
   u1.name AS name1, 
   f.uid2 AS id2, 
   u2.name AS name2
FROM 
   friendships AS f
JOIN game_membership AS user1_gm ON user1_gm.uid = f.uid1
JOIN game_membership AS user2_gm ON user2_gm.uid = f.uid2
JOIN users AS u1 ON f.uid1 = u1.uid
JOIN users AS u2 ON f.uid2 = u2.uid
WHERE user1_gm.gid = <GID> AND user2_gm.gid = <GID>
于 2012-04-15T21:38:33.110 に答える
1
select uid1, u1.name as name1, uid2, u2.name as name2
from friendships 
    join game_membership as g1 on uid1 = g1.uid
    join game_membership as g2 on uid2 = g2.uid
    join users as u1 on uid1 = u1.uid
    join users as u2 on uid2 = u2.uid
where g1.gid = <GID> and g2.gid = <GID>
于 2012-04-15T21:42:03.527 に答える
1

次のようにFROM句に結合を追加します。

FROM friends f
    LEFT OUTER JOIN users u1 ON f.uid1 = u1.uid
    LEFT OUTER JOIN users u2 on f.uid2 = u2.uid
于 2012-04-15T21:38:33.243 に答える
1

別:

 SELECT 
   f.uid1 AS id1, 
   u1.name AS name1, 
   f.uid2 AS id2, 
   u2.name AS name2
FROM friendships AS f
INNER JOIN users AS u1 
   ON f.uid1 = u1.uid
INNER JOIN users AS u2 
   ON f.uid2 = u2.uid
INNER JOIN game_membership GMS
   ON GMS.UID=U1.UID
INNER JOIN game_membership GMS2
   ON GMS2.UID=U2.UID
WHERE 
   GMS.GID= <GID> and GMS2.GID=<GID>

そして今、それはポールの代替手段ではありません:D

于 2012-04-15T21:39:21.823 に答える