分组在mysqli

I have this query:

SELECT *,
COUNT(*) AS SeasonP,
SUM(Goals) AS SeasonG,
SUM(Disposals) AS SeasonD,
SUM(Kicks) AS SeasonK,
SUM(Handballs) AS SeasonHB,
SUM(Marks) AS SeasonM,
SUM(Behinds) AS SeasonB,
SUM(Tackles) AS SeasonT,
SUM(HitOuts) AS SeasonHO,
SUM(I50) AS SeasonI50,
((SELECT COUNT(*) FROM `PlayerDetails` WHERE PlayerID = $PlayerID AND WL LIKE '%W%' )) AS W,
((SELECT COUNT(*) FROM `PlayerDetails` WHERE PlayerID = $PlayerID AND WL LIKE '%L%' )) AS L,
((SELECT COUNT(*) FROM `PlayerDetails` WHERE PlayerID = $PlayerID AND WL='D' )) AS D 
FROM `PlayerDetails` WHERE PlayerID = $PlayerID GROUP BY Season, Team, League

Which almost gives me the correct output - It correctly totals games, goals, kicks, marks etc per season. But what I'm trying to do is display the games won, lost and drawn for each season and for each team. So for eg. 2015 should read Won=12, Lost 5 so just adding the wins and losses for that season but it's displaying the total won/lost for all 32 games his played rather than breaking it down per season.

I'm sure its an easy fix to my query but I'm a bit stuck.

Current query output

Instead of your sub-queries populating W,L,D, use mysql IF() function as described below,

SELECT *, COUNT(*) AS `SeasonP`, SUM(`Goals`) AS `SeasonG`, SUM(`Disposals`) AS `SeasonD`, 
SUM(`Kicks`) AS `SeasonK`, SUM(`Handballs`) AS `SeasonHB`, SUM(`Marks`) AS `SeasonM`,     
SUM(`Behinds`) AS `SeasonB`, SUM(`Tackles`) AS `SeasonT`, SUM(`HitOuts`) AS `SeasonHO`, 
SUM(`I50`) AS `SeasonI50`, 
SUM(IF(`WL` LIKE '%W%', 1, 0)) AS `W`,
SUM(IF(`WL` LIKE '%L%', 1, 0)) AS `L`,
SUM(IF(`WL` = 'D', 1, 0)) AS `D`,    
FROM `PlayerDetails` WHERE `PlayerID` = $PlayerID GROUP BY `Season`, `Team`, `League`

The root of all evil is this one:

((SELECT COUNT(*) FROM `PlayerDetails` WHERE PlayerID = $PlayerID AND WL='...' ))

First of all, you should avoid a SELECT as a value at all, it causes severe performance issues, since it has to be executed for every row in your result - and this may get biiiiig.

In your case, there's no filter condition in the subquery - you always ask "how many games were won EVER", that's why you always get the total value. Your group-by gets skipped this way.

Since you already work with the wanted table "PlayerDetails", try to replace it with this:

sum(if(WL='W',1,0)) AS W, sum(if(WL='D',1,0)) AS D, sum(if(WL='L',1,0)) AS L

Think of it as a counter. For each row it checks the condition and - depending on the result - adds 1 or 0.