I have a database with two tables, tblPlayers and tblMatches.
tblPlayers contains the following fields: playerID, playerName.
tblMatches contains matchID, scorer1ID, scorer2ID, scorer3ID.
I need to run a query to count the total number of goals scored by a player, and return it with their playerID.
I tried the following:
SELECT
tblPlayers.playerID, tblPlayers.playerName, count(*) AS goals
FROM
tblPlayers, matches
WHERE
(tblPlayers.playerID = tblMatches.scorer1ID
OR
tblPlayers.playerID = tblMatches.scorer2ID
OR
tblPlayers.playerID = tblMatches.goalscorer3ID)
GROUP BY tblPlayers.playerID
ORDER BY goals DESC
However, this isn't working as required, as the count function doesn't allow for the fact that a player can score more than one goal in a game (e.g. if a player scores three goals his ID appears in the scorer1ID field, scorer2ID field and scorer3ID field, but the count just returns a value of 1, when it needs to be 3).
Hope that makes sense, I've been tearing my hair out for the last few days trying to solve this! Any help greatly appreciated.