2016-10-27 67 views
-2

比方說,我在MySQL的這兩個表:加入兩個表(含計算)

1. table_name的

name 
------ 
Max  
John 
Sarah 
Peter 

2. table_money

name | amount  | month 
------------------------------- 
Max | 100  | January 
Sarah | 90   | January 
Max | 100  | February 
Sarah | 90   | February 
Peter | 90   | February 

如何有SQL查詢看,這樣我就可以得到這樣的表格:

name | total (amount * #month) 
------------------------------- 
Max | 200 
Sarah | 180 
Peter | 90 
+8

將有一個選擇,一個從,聯接和總數學caluclation。說實話,你還嘗試過什麼,或者你只是想讓我們爲你做? – SomeJavaGuy

回答

2

嘗試使用GROUP BY關鍵字

SELECT table_name.name,sum(table_money.amount) as total 
FROM table_name JOIN table_money 
on table_name.name = table_money.name 
GROUP BY table_money.name 

希望這個作品

+0

@Gordon Linoff感謝您的編輯。 –

+0

它的工作原理,謝謝! – chateaubriand

+0

歡迎。請接受它作爲答案。 –

1
Try this 
SELECT name,sum(amount) Total 
FROM table_money 
GROUP BY name