douchen2025 2013-01-01 14:18
浏览 60
已采纳

子查询格式问题

I am having trouble getting the format correct for a subquery.

One of my queries displays the following:

This involves making calls to three tables (Ind Stats table, that pulls Game Stats, and Teams Name). As you can see, this query works fine.

What I'm having troubles with is how to pull each player that had a DD in that particular game. Players is another able within the database. All I can understand is that I should need a sub-query but sub-queries are not my strong suit.

Here is my code

  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

"

So, to be clear, this works fine:

  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

I'm just trying to display the name of the players who recorded a DD in the row here. I'm getting the name now but it is counting all games that the player played. I'm close now.

enter image description here

UPDATE: I can now display the results in the table but it only shows one player who got a DD. I'm thinking I need a loop to display all the users who have a DD. Any help, much appreciated.

enter image description here

I just changed my above code by adding a GROUP BY stats1_DD and another AND in the FROM Statement as such:

  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
  • 写回答

1条回答 默认 最新

  • dongliang_bj2016 2013-01-01 14:32
    关注

    You should join the third table with the result set that coming from the query you already have or with the three table directly on the target column DD. Something like:

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

    OR:

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

    Update: You will need to JOIN the subquery with the Players table like so:

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c