Hi, i posted the other day about a problem i encountered when making a new site, trouble is i have another one. First here's my table structure:
players
- player_id (primary key)
- player_name
player_points
- player_id
- points
- date_created
teams
- team_id (primary key)
- team_name
team_players
- team_id
- player_id
- date_created
What i need to do is list all the players from the team_players table for a particular team and calculate the number of points accumulated for that player since that player was put in their team.
Here's what i have put together so far:
The trouble i'm having is that if the player was put in the team_players table after any records being inserted into the player_points table then nothing is returned since, this condition:
pp.date_created >= tp.date_created
returns false. I tried changing the query to:
but i got the same result. I thought that using a left outer join on the table would take care of this but i guess there's a mistake in my logic.
Appreciate it if someone could show me what i am doing wrong. Thanks
players
- player_id (primary key)
- player_name
player_points
- player_id
- points
- date_created
teams
- team_id (primary key)
- team_name
team_players
- team_id
- player_id
- date_created
What i need to do is list all the players from the team_players table for a particular team and calculate the number of points accumulated for that player since that player was put in their team.
Here's what i have put together so far:
Code:
SELECT tp.player_id, p.player_name, t.team_name, SUM(pp.points) AS points
FROM team_players tp
LEFT OUTER JOIN players p ON tp.player_id = p.player_id
LEFT OUTER JOIN teams t ON p.team_id = t.team_id
LEFT OUTER JOIN player_points pp ON p.player_id = pp.player_id
WHERE tp.team_id = $team_id AND pp.date_created >= tp.date_created
GROUP BY pp.player_id
ORDER p.player_name
The trouble i'm having is that if the player was put in the team_players table after any records being inserted into the player_points table then nothing is returned since, this condition:
pp.date_created >= tp.date_created
returns false. I tried changing the query to:
Code:
SELECT tp.player_id, p.player_name, t.team_name, SUM(pp.points) AS points
FROM team_players tp
LEFT OUTER JOIN players p ON tp.player_id = p.player_id
LEFT OUTER JOIN teams t ON p.team_id = t.team_id
LEFT OUTER JOIN player_points pp ON p.player_id = pp.player_id AND pp.date_created >= tp.date_created
WHERE tp.team_id = $team_id
GROUP BY pp.player_id
ORDER p.player_name
but i got the same result. I thought that using a left outer join on the table would take care of this but i guess there's a mistake in my logic.
Appreciate it if someone could show me what i am doing wrong. Thanks