我正在编写一个应用程序以保持飞镖得分。我已经创建了一个关系数据库,如下所示:
GameInformation
gameId | numberOfPlayer | totalInnings |游戏类型
0 | 2 | 13 | 0
1 | 2 | 14 | 0
2 | 2 | 20 | 0
3 | 3 | 16 | 0
PlayersByGame
gameId | playerId |局| MPR
0 | 0 | 13 | 1.538
0 | 1 | 13 | 1.651
1 | 0 | 14 | 1.500
1 | 1 | 14 | 2.012
2 | 0 | 20 | 1.658
2 | 2 | 20 | 2.123
3 | 2 | 16 | 2.001
3 | 1 | 16 | 1.325
3 | 0 | 16 | 1.001
球员
名称|标识
Dainon | 0
安迪| 1
基思| 2个
我熟悉简单的SQL查询,但不知道如何获得玩家(Dainon)与玩家(Andy)玩过的游戏数量,而没有超过2名玩家的游戏,只有Dainon和安迪互相对抗。在查询时,我同时拥有Dainon和Andy的玩家ID。
任何人都可以提供任何有关如何编写SELECT语句的见解。
我开始使用错误的查询,例如:
SELECT * FROM PlayersByGame WHERE playerId LIKE 0 and playerId LIKE 1;
SELECT gameId, numberOfPlayers, totalInnings, gameType
FROM GameInformation
WHERE (numberOfPlayers = 2)
UNION
SELECT gameId, playerId, innings, mpr
FROM PlayersByGame
WHERE (playerId = 0)
UNION
SELECT gameId, playerId, innings, mpr
FROM PlayersByGame AS PlayersByGame_1
WHERE (playerId = 1)
谢谢,感谢您的任何帮助!
答案 0 :(得分:1)
我会尝试类似的东西(未经测试)
SELECT count(gi.id)
FROM GameInformation gi
JOIN PlayersByGame pbg1 ON pbg1.gameId=gi.id AND pbg1.playerId = {idPlayer1}
JOIN PlayersByGame pbg2 ON pbg2.gameId=gi.id AND pbg2.playerId = {idPlayer2}
WHERE gi.numberOfPlayer = 2
答案 1 :(得分:1)
试试这个。 点击她查看Sqlfiddle版本
select count(*) as NumOfGames
from
(
select gameid,count(*)
from
(
select gameId,playerId
from PlayersByGame
where gameId in
(
SELECT gameId from GameInformation
where numberOfPlayer =2
)
AND
playerId in
(
select id from Players
where Name in('Dainon','Andy')
)
) as A
group by A.gameId
having count(*) =2
) as B