what you want is a union, not a join
Code:
select c.cust_id
, c.cust_name
, sum(u.buy_amt) as total_buy
from customer as c
left outer
join (
select cust_id
, buy_amount1 as buy_amt
from buy_1
union all
select cust_id
, buy_amount2
from buy_2
) as u
on c.cust_id
= u.cust_id
of course, if you're not on mysql 4.1, you can't do that, so you have to find a workaround
Code:
create temporary table u
( cust_id integer
, buy_amt decimal(9,2)
);
insert into u
select cust_id
, buy_amount1
from buy_1
;
insert into u
select cust_id
, buy_amount2
from buy_2
;
select c.cust_id
, c.cust_name
, sum(u.buy_amt) as total_buy
from customer as c
left outer
join u
on c.cust_id
= u.cust_id
Bookmarks