1

我正在编写一个应用程序来保持飞镖得分。我创建了一个关系数据库,如下所示:

游戏信息 gameId | numberOfPlayer | 总局数 | 游戏类型
0 | 2 | 13 | 0
1 | 2 | 14 | 0
2 | 2 | 20 | 0
3 | 3 | 16 | 0

PlayersByGame 游戏 ID | 玩家ID | 局| 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

球员
姓名 | 伊德
戴农 | 0
安迪 | 1
基思 | 2

我熟悉简单的 SQL 查询,但不知道如何获取与玩家 (Andy) 对战的玩家 (Dainon) 玩过的游戏数量,而游戏中没有超过 2 名玩家且只有 Dainon 和 Andy 玩过互相反对。在查询时,我有 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)

谢谢,感谢您的任何帮助!

4

2 回答 2

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
于 2012-09-12T23:03:32.890 回答
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
于 2012-09-12T23:11:35.007 回答