大家早上好/晚上好,
我正在尝试(左)将两个表加入一个表和匹配语句SUM()
的特定列的值。ON fk_id = id...
这是表格的样子:
ws1 表:
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
字段并将它们加在一起。在示例中,它应该返回3
where player_id
is 4
。我怎样才能做到这一点?
编辑:
表“玩家”:
表“皮卡”: