早上好/晚上好,MySQL如何將兩個JOIN表的SUM()列轉換爲新列?
我想(左)連接兩個表到表和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
只選擇一個相同值的結果。
我的目標是在teamkills
字段中獲得SUM()
s並將它們加在一起。在這個例子中,它應該返回3
,其中player_id
是4
。我怎樣才能做到這一點?
編輯:
表 '球員':
表 '皮卡':
在'pickup'表請出示數據player_id = 4 – peterm
我添加了皮卡和玩家表的截圖。我認爲你所要求的是在'player'表中。 – phew