0

我面临着巨大的危险。我有 2 个表格—— purchaseTbl 和 CustomerTbl ,其中包含:

purchaseTbl : C_ID (int - FK) , Purchase_amt (int)

CustomerTbl:C_ID (int - PK),[其他详细信息]。

所以我想计算两个表中的 C_ID 匹配的所有购买的总和

谢谢

格鲁

4

2 回答 2

0

Use group by clause in your query like this....

SELECT CustomerTbl.C_ID, SUM(Purchase_amt) AS PurchaseSUM FROM CustomerTbl, purchaseTbl WHERE purchaseTbl.C_ID = CustomerTbl.C_ID GROUP BY CustomerTbl.C_ID
于 2013-10-01T07:13:25.817 回答
0
SELECT C.C_ID,
       --You can add more columns (like customer name) here if you wish
       SUM(Purchase_amt) AS SUMP
FROM   CustomerTbl C
       JOIN purchaseTbl P
           ON P.C_ID = C.C_ID
GROUP BY C.C_ID
       --If you added more columns in the select add them here too separated with comma

如果您只想知道总金额而不是将其拆分为客户,那么:

SELECT SUM(Purchase_amt) AS SUMP
FROM   CustomerTbl C
       JOIN purchaseTbl P
           ON P.C_ID = C.C_ID

上面只有在有对应C_ID的情况下才会得到总金额CustomerTbl

于 2013-10-01T07:11:10.283 回答