Welcome to WebmasterWorld Guest from 54.145.167.92

Forum Moderators: open

Message Too Old, No Replies

Combine Sum Total

   
8:23 pm on Apr 4, 2012 (gmt 0)

5+ Year Member



I have a problem that I need help with. I have a MySQL table with name, address, email, and quantity. In this table there are multiple rows where the name, address, and email are the same but the quantity may vary. I need to make a table with the unique name, address, and emails but have the total sum of the quantity for each of those unique name, address, emails. For instance if I have a table with;

Fred, 123rd St, fred@fred.org, 4
Fred, 123rd St, fred@fred.org, 3
Fred, 123rd St, fred@fred.org, 1
George, 489 S. St., george@george.com, 5
George, 489 S. St., george@george.com, 4
George, 489 S. St., george@george.com, 2

I need it to be in one table;
Fred, 123rd St, fred@fred.org, 8
George, 489 S. St., george@george.com, 11

I know how to move the distinct name, address, and email into another table. I just don't know how to add up the sum of the quantity for each unique name, address, and email and insert that into the other table.

I used

INSERT INTO tbl2 (name, address, email) SELECT DISTINCT name, address, email from tbl1;

to remove the duplicates. Can anyone give me an idea to help me add up the sums and insert in to the new table?

Your help would be greatly appreciated.
10:50 pm on Apr 4, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Do a search for the SQL term GROUP BY
 

Featured Threads

Hot Threads This Week

Hot Threads This Month