1

我无法为子查询获取正确的格式。

我的一个查询显示以下内容:

这涉及调用三个表格(Ind Stats 表格,用于提取 Game Stats 和 Teams Name)。如您所见,此查询工作正常。

我遇到的麻烦是如何在特定游戏中拉出每个有 DD 的玩家。玩家是数据库中的另一个能力。我所能理解的是我应该需要一个子查询,但子查询不是我的强项。

这是我的代码

  SELECT * FROM 

        (SELECT game_Date, 
        game_GameID,
        game_SeasonID,
        game_TeamCode,
        teams_Name,
        stats1_GameID,
        Sum(stats1_DD) AS 'TotGameDD'

        FROM game INNER JOIN teams INNER JOIN stats1
        ON 
        teams_TeamCode = game_TeamCode
        AND
        game_GameID = stats1_GameID

        GROUP BY game_GameID
        HAVING Sum(stats1_DD) >1
        ORDER BY Sum(stats1_DD) desc) t

    INNER JOIN  players INNER JOIN stats1 INNER JOIN game 
    ON stats1_GameID = game_GameID
            AND
    sstorm_rawstats1.stats1_PlayerID = sstorm_players.player_PlayerID

"

所以,要清楚,这很好用:

  SELECT game_Date, 
    game_GameID,
    game_SeasonID,
    game_TeamCode,
    teams_Name,
    stats1_GameID,
    Sum(stats1_DD) AS 'TotGameDD'

    FROM game INNER JOIN teams INNER JOIN stats1
    ON 
    teams_TeamCode = game_TeamCode
    AND
    game_GameID = stats1_GameID

    GROUP BY game_GameID
    HAVING Sum(stats1_DD) >1
    ORDER BY Sum(stats1_DD) desc

我只是想在此处的行中显示记录 DD 的玩家的姓名。我现在得到了这个名字,但它正在计算玩家玩过的所有游戏。我现在很近了。

在此处输入图像描述

更新:我现在可以在表格中显示结果,但它只显示一名获得 DD 的玩家。我想我需要一个循环来显示所有拥有 DD 的用户。任何帮助,非常感谢。

在此处输入图像描述

我只是通过在 FROM 语句中添加一个 GROUP BY stats1_DD 和另一个 AND 来更改上面的代码,如下所示:

  FROM game INNER JOIN teams INNER JOIN stats1
    ON 
    teams_TeamCode = game_TeamCode
    AND
    game_GameID = stats1_GameID
    AND
    game_PlayerID = player_PlayerID


    GROUP BY game_GameID, stats1_DD
    HAVING Sum(stats1_DD) >1
    ORDER BY Sum(stats1_DD) desc
4

1 回答 1

0

您应该将第三个表与来自您已经拥有的查询的结果集或直接在目标列上的三个表连接起来DD。就像是:

SELECT * 
FROM YourCrrentQuery t
INNER JOIN ThePlayersTable p ON p.DD = t.DD;

或者:

SELECT * 
FROM YourCrrentQuery t
WHERE DD IN (SELECT DD 
             FROM ThePlayersTable 
             WHERE DD IS NOT NULL);

更新:您将需要像这样JOIN使用表的子查询:Players

SELECT * 
FROM 
(
   SELECT 
     game_Date, 
     game_GameID,
     game_SeasonID,
     game_TeamCode,
     teams_Name,
     stats1_GameID,
     Sum(stats1_DD) AS 'TotGameDD'
     FROM game 
     INNER JOIN teams 
     INNER JOIN stats1  ON teams_TeamCode = game_TeamCode
                       AND game_GameID    = stats1_GameID
     GROUP BY game_GameID
     HAVING Sum(stats1_DD) >1
) t
INNER JOIN players p ON t.TotGameDD = p.DD
ORDER BY t.TotGameDD desc;
于 2013-01-01T14:32:45.513 回答