Here is some code I have which is designed to group by the teams that a player has made appearances against ->
$get_player_totals = mysql_query("
SELECT M.MatchOpponent AS opponent,
COUNT(A.AppearancePlayerID) +
( SELECT COUNT(S.SubstitutionPlayerIDIn)
FROM tplss_substitutions S, tplss_matches M
WHERE S.SubstitutionPlayerIDIn = A.AppearancePlayerID
AND S.SubstitutionPlayerIDIn = '$id'
AND S.SubstitutionMatchID = M.MatchID) AS total_apps
FROM tplss_appearances A, tplss_matches M
WHERE A.AppearanceMatchID = M.MatchID AND
A.AppearancePlayerID = '$id'
GROUP BY M.MatchOpponent
ORDER BY total_apps DESC
LIMIT 5
", $connection) or die(mysql_error());
Can anyone please suggest to me why this example is different any other time I've used this query, and why it is not working? It displays a result, but the result is not correct and is always about 300% more than it should be.
A working example, which shows total_apps figure between two dates ->
$get_player_totals = mysql_query("
SELECT A.AppearancePlayerID AS playerid,
COUNT(A.AppearancePlayerID) +
( SELECT COUNT(S.SubstitutionPlayerIDIn)
FROM tplss_substitutions S, tplss_matches M
WHERE S.SubstitutionPlayerIDIn = A.AppearancePlayerID
AND S.SubstitutionMatchID = MatchID AND
M.MatchDateTime > '19991231' AND
M.MatchDateTime < '20100101') AS total_apps
FROM tplss_appearances A, tplss_matches M
WHERE A.AppearanceMatchID = MatchID AND
M.MatchDateTime > '19991231' AND
M.MatchDateTime < '20100101'
GROUP BY A.AppearancePlayerID
ORDER BY total_apps DESC
LIMIT 5
", $connection) or die(mysql_error());