I have a table that has basically a calendar entry for football games it looks like this
team1 || team1score || team2|| team2score || time || league
I want to update my positions table depending on entries from the calendar table.
so for example if I have in my table something like
Pats || 14 || Saints || 3 || 1stleague
Cows || 7 || falc || 14 || 1stleague
Saints || 31 || falc || 3 || 1stleague
Saints || 14 || cows || 3 || 1stleague
I want a query that will first sum all the values in the team1score column with a distinct team1name. So for the table above it would give as a result
Pats || 14
Cows || 7
Saints || 45
then I want to do the same thing for team2 and team2score so the result would be
Saints || 3
falc || 17
cows || 3
then I want to join these two results and get:
Pats || 14
Cows || 10
Saints || 48
falc || 17
I tried something like this
select distinct(t.team1), sum(t.team1score)
from calendar t
where league = '5a7'
UNION
select distinct(t2.team2), sum(t2.team2score)
from calendar t2
where league = '5a7'
but its not adding the same teams scores any ideas?
I know I'm not taking the sums when I do the union but I think I need to do the union to keep the values that don't have a matching team on the teams2 column. However I can't figure out how to do the union and sum at the same time.