![]() |
SUM() AND COUNT() in same recordset
I have a darts league. I have one table in the database that stores the total scores (results) and one table that holds the individual scores (individResults) and one table that holds the double scores (individResultsDouble). In the league table I need to show the results for each team. It needs to be ordered by overall scores; a win =2 points, a draw =1 point. In the results table I have a column (wld) that stores whether it is a win(w) lose (l) or draw(d).
I have this code which works perfectly: Code:
SELECT rTeamID, SUM( IF(wld='w', 2, IF(wld='d', 1, 0 ) ) ) AS wintotal, team.teamNameCode:
SELECT x.iTeamID, COUNT(x.fa) |
I *think* this does it:
Code:
|
Wow, thank you - I can see all the logic but I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'allresults ON team.TeamID = allresults.rTeamID ORDER BY allresults.wintotal, all' at line 16 |
You'll have to show us the actual query you ran to figure that error out.
|
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 ASIF you do that, then you can test each view independently first: Code:
SELECT * FROM v1; |
You know, looking at all those views, I think I see the problem in my original query.
I think it is missing the SELECT * that I have in the allresults VIEW. So maybe this is all that needs added: Code:
SELECT team.TeamID, team.teamName, allresults.wintotal, allresults.individualWins |
That was it it works perfectly thank you.
|
| All times are GMT +1. The time now is 11:24 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.