I have on table named players, then other tables named tries, conversions, penalties, dropgoals.
I need to get the player_id from players, then count the following:
- number of tries for
player_idintries - number of conversions for
player_idinconversions - number of penalties for
player_idinpenalties - number of dropgoals for
player_idindropgoals
And this all needs to happen in one go as there are about fifteen players for each game, the site relates to rugby.
I have tried the following, which works:
SELECT players.player_id, players.number, CASE WHEN (COUNT(tries.player_id) = 0) THEN ' ' ELSE COUNT(tries.player_id) END AS nrTries FROM players LEFT JOIN tries ON players.player_id = tries.player_id WHERE players.team_id IS NULL GROUP BY players.player_id ORDER BY players.number
It select all players from the players table and counts their tries, but as soon as I change it to the following it gives me an error:
SELECT players.player_id, players.number, player.name, CASE WHEN (COUNT(tries.player_id) = 0) THEN ' ' ELSE COUNT(tries.player_id) END AS nrTries FROM players, player LEFT JOIN tries ON players.player_id = tries.player_id WHERE players.player_id = player.player_id AND players.team_id IS NULL GROUP BY players.player_id ORDER BY players.number
It gives the following error:
Unknown column 'players.player_id' in 'on clause'
Can someone please help me with this, I have been struggling for days now?
Thanks in advance
// edit:
Hi, all, I feel very stupid now, this code works brilliantly, except that when a player has scored in more than one type, say tries and conversions, or tries and penalties, it miscounts the number of each type, and they are made all the same.
Let’s say my player scored 1 penalty and 3 dropgoals, it outputs as 3 penalties and 3 dropgoals, I can’t figure out what’s wrong.
Here is my query:
SELECT players.player_id, players.number, player.name, player.surname,
CASE WHEN (COUNT(tries.player_id) = 0) THEN '& nbsp;' ELSE COUNT(tries.player_id) END AS nrTries,
CASE WHEN (COUNT(conversions.player_id) = 0) THEN '& nbsp;' ELSE COUNT(conversions.player_id) END AS nrConversions,
CASE WHEN (COUNT(dropgoals.player_id) = 0) THEN '& nbsp;' ELSE COUNT(dropgoals.player_id) END AS nrDropgoals,
CASE WHEN (COUNT(penalties.player_id) = 0) THEN '& nbsp;' ELSE COUNT(penalties.player_id) END AS nrPenalties
FROM players
LEFT JOIN tries ON players.player_id = tries.player_id AND tries.game_id = '$game_id'
LEFT JOIN conversions ON players.player_id = conversions.player_id AND conversions.game_id = '$game_id'
LEFT JOIN dropgoals ON players.player_id = dropgoals.player_id AND dropgoals.game_id = '$game_id'
LEFT JOIN penalties ON players.player_id = penalties.player_id AND penalties.game_id = '$game_id'
LEFT JOIN player ON players.player_id = player.player_id
WHERE players.player_id = player.player_id AND players.team_id IS NULL AND players.game_id = '$game_id'
GROUP BY players.player_id ORDER BY players.number
Please note: $game_id is a PHP variable.
Also: I have included a space between & and nbsp; otherwise it does not get ouputted to SO.
Can someone please point me in the right direction?
I’m a little confused why you stated Cletus had the correct solution, and proceeded to not use it while updating your original question. That said, Cletus’ solution is slightly off, you need COUNT() not SUM(). Try the following:
This will return 0’s instead of   I’d recommend handling that in your application code though. You can add in the CASE mess if you really want to get the   from mysql.