2013-07-27 75 views
0

早上好/晚上好,MySQL如何將兩個JOIN表的SUM()列轉換爲新列?

我想(左)連接兩個表到表和SUM()特定列匹配ON fk_id = id...聲明的值。這是該表的樣子:

WS1表:

ws1 table

WS2表:

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只選擇一個相同值的結果。

我的目標是在teamkills字段中獲得SUM() s並將它們加在一起。在這個例子中,它應該返回3,其中player_id4。我怎樣才能做到這一點?

編輯:

表 '球員':

enter image description here

表 '皮卡':

enter image description here

+0

在'pickup'表請出示數據player_id = 4 – peterm

+0

我添加了皮卡和玩家表的截圖。我認爲你所要求的是在'player'表中。 – phew

回答

2

你需要,而不是兩個相關子查詢WS1 + WS2的加入,喬寧不會在這裏工作。
喜歡的東西:

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行重複數據。

本演示中的第三個查詢是一個依賴子查詢的例子,它給出了正確的結果(例如本演示中的數據)。

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 

join查詢和SUM

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 

,我希望你的問題可以通過這種方式解決。

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演示