2012-04-07 115 views
1

我有兩個表Loan和Member。現在在貸款我有列totalamount和成員我有專欄作爲membertype和貸款我有各種貸款根據membertype。現在我想使用SUM函數根據memtype計算totalamount。SQL查詢SUM函數

我試過的東西如下:

select sum(totalamount) from loan,member where member.mem_type='Regular' 
+0

成員表的memberid是貸款表中的外鍵表 – 2012-04-07 11:34:12

+0

請使用[ANSI 92連接形式語法]( http://stackoverflow.com/questions/334201/why-isnt-sql-ansi-92-standard-better-adopted-over-ansi-89)。 – 2012-04-07 11:45:47

回答

1

你的意思是這樣的:

select sum(totalamount) AS Total 
from loan 
JOIN member ON Memberid=loan.Memberid 
where member.mem_type='Regular' 

或者,如果你想選擇mem_type還有那麼這樣的事情:

select sum(totalamount) AS Total,member.mem_type 
from loan 
JOIN member ON Memberid=loan.Memberid 
where member.mem_type='Regular' 
GROUP BY member.mem_type 
+0

謝謝一噸阿里恩 – 2012-04-07 11:51:29

+0

沒問題。很高興幫助:P – Arion 2012-04-07 11:51:56