duankang5285 2015-11-05 04:53
浏览 37
已采纳

添加2个或更多不同行的列值。 SQL

I have a table with the following scheme: http://i.stack.imgur.com/zH2zz.png
This table is for a hockey league (Link Here). This is the second season where stats will be held in a database. If you look select the dropdowns in the link, you will see that I can produce a top 20 lists for both seasons (1 season has no data yet other than 1 tester). The goal is to total the two seasons and have a All Time top 20 list. I have tried using a sum function, but that literally added every single row into 1.

I only want to sum the rows of repeated player id's. For example, I am player id #2 and I need every goal and assist added per row containing my PID, but I need this for all the top 20 players in 1 query. Here are some of the queries I am using thus far.

SELECT Rosters.PID, Rosters.Goals, Rosters.Assists, Rosters.PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
      (Rosters.Goals + Rosters.Assists) AS Points
      FROM Rosters
      INNER JOIN Players
      ON Rosters.PID = Players.pid
      WHERE Rosters.TID BETWEEN $parameter
      ORDER BY Points DESC, Goals DESC
      LIMIT 0,20

The $parameter is based on the team id (TID) There are 16 tid's (8 for each season). This query gets all of the records when the parameter is 1-16, it just doesn't add up non distinct rows by PID (This is ultimately what I would like to learn to do). This is the drop down showing how the query is being instantiated

<div class="ddstyle">
    <select name="DropDownTeams" id="DDTeams">
        <option>Select a Season</option>
        <option value="stats2.php?tid=9 AND 16">2015-2016</option>
        <option value="stats2.php?tid=1 AND 8">2014-2015</option>
        <option value="stats2.php?tid=1 AND 16">All Time</option>
    </select>
</div>

I tried to do:

SELECT Rosters.PID, SUM( Rosters.Goals ) Goals, SUM( Rosters.Assists ) Assists, SUM( Rosters.PIM ) PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname, 
    SUM((Rosters.Goals + Rosters.Assists)) AS Points
    FROM Rosters
    INNER JOIN Players
    ON Rosters.PID = Players.pid
    WHERE Rosters.TID BETWEEN 1 AND 16
    ORDER BY Points DESC, Goals DESC
    LIMIT 0,20;

But that just added up every record into a single row. The Where clause obviously is not working the way I want when used with the SUM() functions.

  • 写回答

1条回答 默认 最新

  • doude1917 2015-11-05 04:57
    关注

    Include the Group BY section http://www.w3schools.com/sql/sql_groupby.asp

    SELECT Rosters.PID
        ,SUM(Rosters.Goals) Goals
        ,SUM(Rosters.Assists) Assists
        ,SUM(Rosters.PIM) PIM
        ,Players.pid
        ,SUM((Rosters.Goals + Rosters.Assists)) AS Points
    FROM Rosters
    INNER JOIN Players ON Rosters.PID = Players.pid
    WHERE Rosters.TID BETWEEN 1
            AND 16
    GROUP BY 
        Players.pid
    ORDER BY Points DESC
        ,Goals DESC 
    LIMIT 0
        ,20;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?