Let's start off from the point of view of what is the relational theory tells us is the right approach.
Team.Points is redundant and breaks data normalisation so it should not be in your normalised design.
You say that the problem in working out team points is that it is the sum of the active players points (which will be 11) rather than all of a team,s (17) players. So your data model needs to capture whether a player is active on the team or not. This should be added to the table Lookup.
Lookup(tid, pid, active)
The mysql query for this would be:
Code:
CREAT TABLE Lookup (
tid INT UNSIGNED NOT NULL,
pid INT UNSIGNED NOT NULL,
active CHAR(1),
PRIMARY KEY ( tid, pid )
);
With my modification in mind - if I understand you correctly - then to work out the total for $teamName you would use the query:
Code:
SELECT SUM(Player.Points) AS TeamPoints
FROM Player, Team, Lookup
WHERE Player.ID = PID
AND Team.ID = TID
AND active = 'Y'
AND Team.Name = '$teamName'
Well that's how you would select the TeamPoints for one team. To create your team league table you would use this query...
Code:
SELECT Team.Name AS TeamName, SUM(Player.Points) AS TeamPoints
FROM Player, Team, Lookup
WHERE Player.ID = PID
AND Team.ID = TID
AND active = 'Y'
GROUP BY TeamName
ORDER BY TeamPoints DESC
Now back to your original idea of holding the team points in a seperate field of table Teams. Like I said, this breaks data normalisation and what I have outlined above is the way you should do this in theory.
If executing those queries in real time becomes too inefficient then you can look at "caching" the team points in a seperate field per your original design. Here you would run one of my queries from above and then in your PHP use the value in the result set for each team to in a seperate UPDATE query to update Teams.Points . But, unless you are dealing with massive tables then I don't think you will have a problem executing the joins in the queries I have given above in real time.
Bookmarks