0

大家早上好/晚上好,

我正在尝试(左)将两个表加入一个表和匹配语句SUM()的特定列的值。ON fk_id = id...这是表格的样子:

ws1 表:

ws1 表

ws2 表:

ws2 表

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

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

结果:

查询结果

和:

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

结果:

查询结果

我知道SUM(DISTINCT.... )返回2,因为DISTINCT只选择一个相同值的结果。

我的目标是获取SUM()两个teamkills字段并将它们加在一起。在示例中,它应该返回3where player_idis 4。我怎样才能做到这一点?

编辑:

表“玩家”:

在此处输入图像描述

表“皮卡”:

在此处输入图像描述

4

3 回答 3

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,查看第一个查询(和下面的结果集)以更好地理解为什么会从连接中得到错误的结果,以及连接是如何工作的。

Join 将一个表中的每条记录组合(粘合)到另一个表中的所有相应记录(符合连接条件),在您的情况下,它会产生 4 行带有重复数据的行。

本演示中的第三个查询是一个给出正确结果的相关子查询示例(例如本演示中的数据)。

于 2013-07-27T04:59:48.273 回答
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

样本输出:

| 别名 | 团队杀戮 |
----------------------
| 别名4 | 3 |

这是SQLFiddle演示

于 2013-07-27T05:15:32.373 回答
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

我希望您的问题将通过这种方式解决。

于 2013-07-27T05:01:32.657 回答