Page 1 of 1

SUM from multiple tables

Posted: Wed Sep 06, 2006 12:54 pm
by bluesman333
I want to select a sum of values...but from multiple tables.

Code: Select all

$sql = "
SELECT SUM(a.sales_type1), SUM(b.sales_type2)
FROM type1 AS a, type2 AS b
WHERE a.id = b.id";
The problem I am having is that there is a different number of rows in each table and the JOIN is adding rows to the other table

I also need to add a customer table to this statement and GROUP BY the customer number or customer state

Posted: Wed Sep 06, 2006 2:55 pm
by feyd
SQL is not PHP. :roll:

Posted: Wed Sep 06, 2006 3:17 pm
by wtf
What version of mysql do you use?

You could create a UNION

you could do something like this

create view sumofall as
select salestype from a
union
select salestype from b
union
select salestype from c


then

select sum(salestype) from sumofall

I'm sure it could also be done with some subqueries