Need a routine for listing sum of values in another table

Here is a simple example illustrating my problem:

I have two tables in my MySQL database

Table1
[table]
[tr]
[td]Name[/td]
[td]Number[/td]
[/tr]
[tr]
[td]Jane[/td]
[td]4 [/td]
[/tr]
[tr]
[td]Mark[/td]
[td]6 [/td]
[/tr]
[tr]
[td]Jane[/td]
[td]3[/td]
[/tr]
[tr]
[td]Sara[/td]
[td]7 [/td]
[/tr]
[tr]
[td]Jane[/td]
[td]6[/td]
[/tr]
[tr]
[td]Mark[/td]
[td]2[/td]
[/tr]
[/table]

I want to make summary for each person, and present the result in a new table, like this:

Table2
[table]
[tr]
[td]Name[/td]
[td]Sum[/td]
[/tr]
[tr]
[td]Jane[/td]
[td]13[/td]
[/tr]
[tr]
[td]Mark[/td]
[td]8[/td]
[/tr]
[tr]
[td]Sara[/td]
[td]7[/td]
[/tr]
[/table]

How can I make a php/MySQL routine for this?

There is no real need to store it into a new table. This query does the work for you

SELECT Name, SUM(Number) as Sum FROM test GROUP BY Name

NAME SUM Jane 13 Mark 8 Sara 7

test here:
http://sqlfiddle.com/#!2/17d102/2

Sponsor our Newsletter | Privacy Policy | Terms of Service