0

I am looking for an elegant solution to a seemingly simple MySQL problem:

I've got a table with soccer teams and game results from a soccer league.

league  day  team1   team2      points1    points2
bl0910  1    munich  hamburg    3          0
bl0910  1    bremen  stuttgart  1          1
...

Now I want to add a column, where all points of a team from all previous game days are summed up.

Like:

league  day  team1   team2      points1    points2   sum1    sum2
...
bl0910  8    bremen  hamburg    1          1         11      15
bl0910  8    munich  stuttgart  0          3         16      9
...

Is it possible with some elegant syntax, or do I have to use cursors and a temporary table?

Kind Regards Theo

Theo H.
  • 141
  • 1
  • 3
  • 8
  • I'd need bigger data sample to check expected results. – Horaciux Oct 29 '14 at 23:03
  • Use [Triger after insert](http://stackoverflow.com/questions/16892070/mysql-after-insert-trigger-which-updates-another-tables-column). – Payam Oct 30 '14 at 00:20
  • I wouldn't store derived data in a table. Create the sums when you query it. If you *really* need a query you'll have to use a sub-subselect to get around the restrictions with INSERT-SELECT – ethrbunny Oct 30 '14 at 01:41

0 Answers0