MySQL如何将两个JOINed表的SUM()列放入一个新列?

时间:2013-07-27 03:46:20

标签: mysql join sum

每个人早上好/晚上好,

我正在尝试(左)将两个表连接到一个表中,并匹配SUM()语句的ON fk_id = id...个特定列的值。这就是表格的样子:

ws1 table:

ws1 table

ws2 table:

ws2 table

到目前为止我尝试过的查询:

SELECT
    alias.name alias,   
    (SUM(IFNULL(ws1.teamkills,0)) + SUM(IFNULL(ws2.teamkills,0))) teamkills
FROM pickup
    JOIN player ON player.pickup_id = pickup.id
    JOIN alias ON player.alias_id = alias.id
    LEFT JOIN weapon_stats_1 ws1 ON ws1.pickup_id = pickup.id AND ws1.player_id = player.id
    LEFT JOIN weapon_stats_2 ws2 ON ws2.pickup_id = pickup.id AND ws2.player_id = player.id
WHERE pickup.logfile_name = 'srv-20130725-2151-log' GROUP BY player.id

结果:

query result

SELECT
    alias.name alias,   
    (SUM(DISTINCT IFNULL(ws1.teamkills,0)) + SUM(DISTINCT IFNULL(ws2.teamkills,0))) teamkills
FROM pickup
    JOIN player ON player.pickup_id = pickup.id
    JOIN alias ON player.alias_id = alias.id
    LEFT JOIN weapon_stats_1 ws1 ON ws1.pickup_id = pickup.id AND ws1.player_id = player.id
    LEFT JOIN weapon_stats_2 ws2 ON ws2.pickup_id = pickup.id AND ws2.player_id = player.id
WHERE pickup.logfile_name = 'srv-20130725-2151-log' GROUP BY player.id

结果:

Query result

我了解SUM(DISTINCT.... )会返回2,因为DISTINCT只选择一个具有相同值的结果。

我的目标是获取两个SUM()字段的teamkills个并将它们添加到一起。在示例中,它应返回3,其中player_id4。我怎么能这样做?

编辑:

表'玩家':

enter image description here

表'拾取':

enter image description here

3 个答案:

答案 0 :(得分:2)

你需要两个从属子查询而不是ws1 + ws2的连接,jonin不会在这里工作。
类似的东西:

SELECT id, player_alias,
       ( SELECT sum( teamkills ) FROM ws1
         WHERE ws1.player_id = player.id )
        +
       ( SELECT sum( teamkills ) FROM ws2
         WHERE ws2.player_id = player.id ) as total
FROM player
JOIN alias ON ......

这是SQLFiddle demo,请查看第一个查询(以及下面的结果集),以便更好地了解为什么从连接中得到错误的结果,以及一般来说,连接是如何工作的。

加入将来自一个表的每个记录组合(胶合)到另一个表中的所有相应记录(满足连接标准),并且在您的情况下,它产生具有重复数据的4行。

本演示中的第三个查询是依赖子查询的示例,它提供了正确的结果(例如本演示中的数据)。

答案 1 :(得分:0)

你可能会跟随

表t1

 CREATE TABLE `t1` (
      `pik_id` int(11) NOT NULL AUTO_INCREMENT,
      `palyer_id` int(11) DEFAULT NULL,
      `amount` double DEFAULT NULL,
      UNIQUE KEY `pik_id` (`pik_id`)
    )

 ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

表t2

    CREATE TABLE `t2` (
  `playayer_id` int(11) NOT NULL AUTO_INCREMENT,
  `amount` double DEFAULT NULL,
  UNIQUE KEY `playayer_id` (`playayer_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

以及joinSUM

的查询
SELECT playayer_id, t1.amount+t2.amount amount
FROM
(SELECT t1.pik_id,t1.palyer_id,SUM(t1.amount) amount FROM t1 GROUP BY t1.palyer_id)t1
JOIN 
(SELECT t2.playayer_id,t2.amount FROM t2)t2
ON t1.palyer_id=t2.playayer_id
GROUP BY playayer_id


    playayer_id amount
           1    133
           2    152
           3    1076

我希望你的问题能够通过这种方式解决。

答案 2 :(得分:0)

不使用相关子查询的可能解决方案

SELECT a.name alias, SUM(q.teamkills) teamkills
  FROM
(
  SELECT player_id, teamkills
    FROM weapon_stats_1 w JOIN pickup p
      ON w.pickup_id = p.id
   WHERE p.logfile_name = 'srv-20130725-2151-log'
   UNION ALL
  SELECT player_id, teamkills
    FROM weapon_stats_2 w JOIN pickup p
      ON w.pickup_id = p.id
   WHERE p.logfile_name = 'srv-20130725-2151-log'
) q JOIN player p
    ON q.player_id = p.id JOIN alias a
    ON p.alias_id = a.id
 GROUP BY a.name

示例输出:

|  ALIAS | TEAMKILLS |
----------------------
| alias4 |         3 |

这是 SQLFiddle 演示