2014-04-25 39 views
-2

我有兩個表:產品 & 銷售如何使用組,內部連接和總和?

產品

id- productname - grp 

11 bmwblue  bmw 
12 bmwwhite  bmw 
13 audiblack  audi 
14 audiwhite  audi 

銷售

id productid amount 

1  11  2 
2  12  1 
x  13  5 
x  14  3 

我怎樣才能顯示;

audi 8,bmw 3?

+2

你嘗試過什麼嗎? – Strawberry

回答

0
SELECT grp, 
     sum(amount) AS total_grp_amount 
FROM products AS p 
JOIN sales AS s ON p.id = s.product_id 
GROUP BY grp 

我想你正在尋找這個。