Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 422983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:08:06+00:00 2026-05-12T19:08:06+00:00

I have a mysql database that is tracking hockey stats. What I’d like to

  • 0

I have a mysql database that is tracking hockey stats. What I’d like to do is in one query get the number of goals and assists scored by each player as well as the number of games that they’ve played in. I’m using Zend Framework and the query that I’ve build is this:

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`

This query returns me data, but my counts are off.

+-----------+---------------+-------+-------+---------+
| 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 | 
+-----------+---------------+-------+-------+---------+

In this dataset the most games that should be present are 6, but as you can see I’m getting extras. If I adjust my query to remove the goals and assists fields my games count comes out correct. In fact if I only select one of my counted rows I always get the correct counts, but once I add a second or third count my numbers start to get skewed. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:08:07+00:00Added an answer on May 12, 2026 at 7:08 pm

    Since you are doing multiple joins which may each match multiple rows and carry over to the next join, you’ll need to add distinct in your count. Try this:

    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`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database that contains a large number of polygons, one for
I have a mysql database that looks like this: |id | city | created
I have several columns in a MySQL database that I would like to add
I have a MySQL database that looks like this: users ( id , name
I have a MySQL database that keeps track of sports stats. I'm trying to
I have a MySQL database that I'd like to test the performance of. I
I have a MySQL Database That Looks like This: Name: Joe Items: 25 CreationDate:
We have a MySQL database that is set up on one of our servers.
I have a mysql database that has text stored in it with non encoded
I have a MySQL database that I am keeping temperature readings from several different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.