I have the following code:
$get_player_totals = mysql_query("SELECT
COUNT(A.AppearancePlayerID) + COUNT(S.SubstitutionPlayerIDIn) AS totalapps,
P.PlayerID AS playerid,
P.PlayerLastName AS player
FROM player_appearances A, player_substitutions S, players P
WHERE P.PlayerID = A.AppearancePlayerID
AND P.PlayerID = S.SubstitutionPlayerIDIn
GROUP BY 'playerid'
ORDER BY 'totalapps' DESC
", $connection) or die(mysql_error());
What it's meant to do is combine full appearances with substitute appearances of football players and display them as one total. At the moment I get a result but its an astronomically incorrect total for each player.
The tables are built as follows:
player_appearances (2 fields - AppearanceID & AppearancePlayerID)
player_substitutions (2 fields - SubstitutionID & SubstitutionPlayerIDIn)
players (3 fields - PlayerID, PlayerFirstName, PlayerLastName)
Ideally, I want to display rows for each player, with the following 2 columns:
Name | Total Apps (Apps and Subs)
Total Apps with the sum of all of their appearances and substitutions.
Where am I going wrong in combining the two counts, and if there is a better way, could someone please provide an example that allows me to sort the rows by the total apps?
Thanks in advance.