2009-10-20 40 views
1

我有一個跟蹤曲棍球統計數據的mysql數據庫。我想要做的是在一個查詢中獲得每個玩家獲得的目標和助攻數量以及他們玩過的遊戲數量。我使用Zend Framework和我構建的查詢是這樣的:計數多個表中的行

SELECT `p`.*, 
     `pxt`.`jersey_number`, 
     count(pxg.player_x_game_id) AS `games`, 
     count(goals.scoring_id) AS `goals`, 
     count(assists.scoring_id) AS `assists` 
FROM `players` AS `p` 
INNER JOIN `players_x_teams` AS `pxt` ON p.player_id = pxt.player_id 
INNER JOIN `teams_x_seasons` AS `txs` ON pxt.team_id = txs.team_id 
INNER JOIN `seasons` AS `s` ON txs.season_id = s.season_id 
INNER JOIN `games` AS `g` ON g.season_id = s.season_id 
INNER JOIN `players_x_games` AS `pxg` ON pxg.game_id = g.game_id 
            AND pxg.player_id = p.player_id 
LEFT JOIN `scoring` AS `goals` ON goals.game_id = g.game_id 
           AND goals.scorer_id = p.player_id 
LEFT JOIN `scoring` AS `assists` ON assists.game_id = g.game_id 
           AND (assists.assist1_id = p.player_id OR assists.assist2_id = p.player_id) 
WHERE (pxt.team_id = 1) 
    AND (txs.season_id = '23') 
    AND (pxt.date_added <= s.end_date OR pxt.date_added is null) 
    AND (pxt.date_removed >= s.start_date OR pxt.date_removed is null) 
GROUP BY `p`.`player_id` 

此查詢返回我的數據,但我的計數是關閉的。

+-----------+---------------+-------+-------+---------+ 
| player_id | jersey_number | games | goals | assists | 
+-----------+---------------+-------+-------+---------+ 
|   2 | 3    |  7 |  1 |  3 | 
|   3 | 19   |  6 |  1 |  0 | 
|   8 | 8    |  7 |  3 |  2 | 
|   9 | 11   | 13 | 10 |  8 | 
|  11 | 96   |  6 |  1 |  3 | 
|  12 | 14   |  6 |  0 |  3 | 
|  13 | 7    |  6 |  0 |  1 | 
|  115 | 39   |  9 |  6 |  2 | 
|  142 | 68   |  6 |  0 |  1 | 
|  143 | 30   |  6 |  0 |  0 | 
|  150 | 41   | 11 | 11 |  5 | 
|  185 | 17   |  6 |  6 |  3 | 
|  225 | 97   |  4 |  1 |  3 | 
+-----------+---------------+-------+-------+---------+ 

在這個數據集的大部分遊戲應該是目前有6個,但你可以看到我越來越演員。如果我調整我的查詢來移除目標和助攻字段,我的遊戲計數就會正確。事實上,如果我只選擇一個計數的行,我總是可以得到正確的計數,但是一旦我添加第二個或第三個計數,我的數字就會開始偏斜。我究竟做錯了什麼?

回答

3

由於您正在做多個連接,它們可能每個匹配多個行並繼續下一次加入,您需要在您的count中添加distinct。試試這個:

SELECT `p`.*, 
     `pxt`.`jersey_number`, 
     count(distinct pxg.player_x_game_id) AS `games`, 
     count(distinct goals.scoring_id) AS `goals`, 
     count(distinct assists.scoring_id) AS `assists` 
FROM `players` AS `p` 
INNER JOIN `players_x_teams` AS `pxt` ON p.player_id = pxt.player_id 
INNER JOIN `teams_x_seasons` AS `txs` ON pxt.team_id = txs.team_id 
INNER JOIN `seasons` AS `s` ON txs.season_id = s.season_id 
INNER JOIN `games` AS `g` ON g.season_id = s.season_id 
INNER JOIN `players_x_games` AS `pxg` ON pxg.game_id = g.game_id 
            AND pxg.player_id = p.player_id 
LEFT JOIN `scoring` AS `goals` ON goals.game_id = g.game_id 
           AND goals.scorer_id = p.player_id 
LEFT JOIN `scoring` AS `assists` ON assists.game_id = g.game_id 
           AND (assists.assist1_id = p.player_id OR assists.assist2_id = p.player_id) 
WHERE (pxt.team_id = 1) 
    AND (txs.season_id = '23') 
    AND (pxt.date_added <= s.end_date OR pxt.date_added is null) 
    AND (pxt.date_removed >= s.start_date OR pxt.date_removed is null) 
GROUP BY `p`.`player_id` 
3

也許您需要count(DISTINCT pxg.player_x_game_id) ...?看起來有可能是在那個humugous megajoin(我承認我並沒有真正花時間完全重現!)的重複 - )...

+0

不同的是。我想我已經阻止了與我的連接邏輯重複,但我想我錯過了一些東西。謝謝。 –

+0

@Rob,不客氣! –