简体   繁体   English

基于来自另一个表的条件的SUM列

[英]SUM columns based on condition from another table

I have two tables. 我有两张桌子。

Table 1 is: 表1是:

Week / Team  / Player  / Stat1 / Stat2 / Stat3 / ...<br>
   1 / Team1 / Player1 / #     / #     / #     / ...<br>
   1 / Team2 / Player2 / #     / #     / #     / ...

Table 2 is: 表2是:

Player  / Team  / Played Game / Tiebreaker /
Player1 / Team1 / N           / #
Player2 / Team2 / Y           / #

Where Played Game either displays a Y means they have played at least one game and N means they haven't played any. 如果“玩过的游戏”显示为Y,则表示他们至少玩过一场游戏;如果显示N,则表示他们没有玩过任何游戏。

What I'm trying to do is sum the stats by column for each player from Table 1 and add their tiebreaker value from Table 2 to it, but I want a condition where if Played Game is equal to N, it just returns 0 as the value. 我想要做的是将表1中每个玩家的统计信息按列求和,并将表2中的决胜局值添加到表中,但是我想要一个条件,如果玩过的游戏等于N,则仅返回0作为值。 Hopefully this makes sense. 希望这是有道理的。

This is my code: 这是我的代码:

$sql = "SELECT Player, 

SUM (Stat1) AS Stat1,
SUM (Stat2) AS Stat2,
SUM (Stat3) AS Stat3

FROM Table 1

GROUP BY Player
ORDER BY Stat1 DESC";

Which obviously just sums the values up for me. 显然这只是为我总结了价值。 My problem is I'm not sure how to retrieve the information from the other table and do what I want in the same query. 我的问题是我不确定如何从另一个表中检索信息并在同一查询中执行所需操作。 I've tried a CASE WHEN and IF statement with the sum, but hasn't worked. 我已经尝试使用CASE WHEN和IF语句求和,但是没有用。 I'm new, so I'm trying to learn, but I'm currently stuck. 我是新手,所以我想学习,但是我目前仍处于困境。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You want conditional summation, which is using a CASE statement as the argument for an aggregation function: 您需要条件求和,它使用CASE语句作为聚合函数的参数:

SELECT t1.Player, 
       (SUM(CASE WHEN t2.Played = 'Y' THEN Stat1 ELSE 0 END) +
        MAX(CASE WHEN t2.Played = 'Y' THEN TIEBREAKER ELSE 0 END)
       ) AS Stat1,
       SUM(CASE WHEN t2.Played = 'Y' THEN Stat2 ELSE 0 END) AS Stat2,
       SUM(CASE WHEN t2.Played = 'Y' THEN Stat3 ELSE 0 END) AS Stat3
FROM Table1 t1 LEFT JOIN
     Table2 t2
     ON t1.player = t2.player AND t1.team = t2.team
GROUP BY t1.Player
ORDER BY Stat1 DESC;

To get data from both tables include the other table in 要从两个表中获取数据,请在

`FROM table1, table2` 

and join them with 并与他们一起

`Where Table1.Player=Table2.Player`

That way, in the result the Player column will have every player once, the sum columns (as Stat1, Stat2, Stat3) will be summing the Stat column for each record in Table2 that has a corresponding Player in Table1 (which is all of them). 这样,结果Player列将为每个玩家播放一次,sum列(如Stat1,Stat2,Stat3)将为Table2中每个记录的Stat列求和,该记录在Table1中具有对应的Player(所有这些都)。

I don't know the php part, and I don't know what you mean by 我不知道php部分,也不知道你的意思

I only want the tiebreaker added once, after the stats have been summed. 在统计数据汇总后,我只希望平局一次。

but I think this query will work right for the rest. 但我认为此查询将在其余时间正常工作。

`SELECT Table2.Player, #I assume Table1.Player has repeat values and stuff,
SUM ( if ( Table2.PlayedGame="Y", Table1.Stat1, 0) ) AS Stat1,
SUM ( if ( Table2.PlayedGame="Y", Table1.Stat2, 0) ) AS Stat2,
SUM ( if ( Table2.PlayedGame="Y", Table1.Stat3, 0) ) AS Stat3
FROM Table1, Table2
Where Table1.Player = Table2.Player
GROUP BY Player
ORDER BY Stat1 DESC";`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM