0

我有 4 个表如下,;

tbl_UserInfo        DUMMY VALUES        DATATYPE    
UserID                 1                Integer (PRIMARY KEY)   
UserName               A                Text        
FacebookID          10021556            Text        
EmailID             ABC@gmail.com       Text        
Facebook_EmailID    asdf@asfd.com       Text        
Password            welcome123          Text        
Chips                   100             Integer     
DeviceType             iphone           Text    



 tbl_Game           DUMMY VALUES        DATA TYPE   
 GameID                 1               Integer     
 Type                 H or M            Text        
 Complete               No              Text    



tbl_GamePlayer      DUMMY VALUES        DATA TYPE
ID                      1           Integer (PRIMARY KEY)
GameID                  1           Integer (FOREIGN KEY FOR GameID in tbl_Game)
userid                  1           Integer (FOREIGN KEY FOR UserID in tbl_UserInfo)    



tbl_Streaks          DUMMY VALUES       DATA TYPE
ID                     1            Integer
GameID                 1            Integer (FOREIGN KEY FOR GameID in tbl_Game)
Player1                1            Integer (FOREIGN KEY FOR UserID in tbl_UserInfo)
Player2                2            Integer (FOREIGN KEY FOR UserID in tbl_UserInfo)
Streaks                2            Integer

我想要reslut作为

{
Player1 = "A"
Player2 = 'B"
Streaks = 10 (between Player1  and Player2)
Chips = 100 (for player1)
Chips = 200 (for player2)
Fbid= 124578 (for player1)
Fbid = 784512 (for player2)
}

{
Player1 = "A"
Player3 = 'C"
Streaks = 15 (between Player1  and Player3)
Chips = 100 (for player1)
Chips = 450 (for player3)
Fbid= 5654578 (for player1)
Fbid = 7845512 (for player3)
}

{
Player1 = "A"
Player4 = 'D"
Streaks = 5(between Player1  and Player4)
Chips = 100 (for player1)
Chips = 208 (for player4)
Fbid= 12898978 (for player1)
Fbid = 78488512 (for player4)
}

我想显示已登录用户的所有现有游戏(以及他其他玩家的 fbid、用户名、筹码、条纹),完整 = '否'

注意 -userid作为参数传递,所有玩家都通过他们的用户 ID 连接

4

1 回答 1

1
SELECT Player1.UserName    AS Player1_UserName,
       Player2.UserName    AS Player2_UserName,
       tbl_Streaks.Streaks AS Streaks,
       Player1.Chips       AS Player1_Chips,
       Player2.Chips       AS Player2_Chips,
       Player1.FacebookID  AS Player1_FacebookID,
       Player2.FacebookID  AS Player2_FacebookID
FROM   tbl_Game
  JOIN tbl_GamePlayer AS gPlayer1 ON gPlayer1.GameID  = tbl_Game.GameID
  JOIN tbl_GamePlayer AS gPlayer2 ON gPlayer2.GameID  = tbl_Game.GameID
                                 AND gPlayer1.UserID != gPlayer2.UserID
  JOIN tbl_UserInfo   AS  Player1 ON gPlayer1.UserID  =  Player1.UserID
  JOIN tbl_UserInfo   AS  Player2 ON gPlayer2.UserID  =  Player2.UserID
  LEFT JOIN tbl_Streaks ON tbl_Streaks.GameID = tbl_Game.GameID
                       AND (tbl_Streaks.Player1, tbl_Streaks.Player2) IN (
                             (Player1.UserID, Player2.UserID),
                             (Player2.UserID, Player1.UserID)
                           )
WHERE  Player1.UserID = 1 AND tbl_Game.Complete = 'No'

sqlfiddle上查看。

于 2012-06-04T12:11:45.707 回答