Friday, 15 February 2013

mysql - join two columns with same title but in different table -



mysql - join two columns with same title but in different table -

i have table pur values

cust_id amount 1 100 3 50 1 200 1 500 3 20

and table cus values as:

cust_id 1 2 3

how show this:

cust_id total_amount 2 0 3 70 1 800

simply put, how show 2 columns cus table?

you can utilize left join , sum of amount grouping rows same cust_id :

select t2.cust_id , coalesce(sum(t1.amount), 0) total cus t2 left bring together pur t1 on t1.cust_id = t2.cust_id grouping t2.cust_id order total sqlfiddle

mysql sql join

No comments:

Post a Comment