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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?