2

表结构:

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

基本上我试图获取一个作为函数的查询gid,并返回:

uid1, name1, uid2, name2

其中仅返回给定表uid中的 's 。这是我到目前为止所得到的:game_membershipgid

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在表中的 's上再做一个或两个 JOIN game_membership,每个一个uid

4

4 回答 4

9

我建议你加入 Game_membership 表而不是 where 子句:

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

像这样向您的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 回答
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 回答