View Single Post
Old 09-19-2012, 11:42 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
It may just be MySQL being stupid, yet again.

One thing that might help is to convert some of those inner queries into VIEWs and then join the views.

For example:
Code:
CREATE VIEW v1 AS
SELECT rTeamID, SUM( IF(wld='w', 2, IF(wld='d', 1, 0 ) ) ) AS wintotal
FROM results
GROUP BY rTeamID;

CREATE VIEW v2 AS
SELECT iTeamID FROM individResults WHERE fa='f'
UNION ALL 
SELECT iTeamID FROM individResultsDoubles WHERE fa='f';

CREATE VIEW v3 AS
SELECT iTeamID, COUNT(*) AS individualWins
FROM v2
GROUP BY iTeamID;

CREATE VIEW allresults AS
SELECT * FROM v1 INNER JOIN v3 ON v1.rTeamID = v3.iTeamID;

SELECT team.TeamID, team.teamName, allresults.wintotal, allresults.individualWins
FROM team LEFT JOIN allResults ON team.TeamID = allresults.rTeamID
ORDER BY allresults.wintotal, allresults.individualWins
(You can and should use better view names than just v1, v2, v3.)

IF you do that, then you can test each view independently first:
Code:
SELECT * FROM v1;
SELECT * FROM v2;
SELECT * FROM v3;
SELECT * FROM allresults;
If they all look good, then the final query should be fine.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote