我有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
我希望重新开始
{
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)
}
我希望显示已登录用户的所有现有游戏(与其他玩家的fbids,用户名,筹码,条纹)完全='否'
注意 - userid
作为参数传递,所有玩家都通过其用户ID
答案 0 :(得分: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上查看。