1
A table
--------------------
id bId        txnVolume
1  1(b table) 10.00
2  1          5.00
3  2          7.00
4  3          2.00

B table
--------------------
id cId
1  1(C table)
2  2
3  3

C table
--------------------
id cusId prodId
1  1     1
2  1     2
3  1     1
4  1     2

如果 C 表是 custId 1 和 prodId 1,我想获得表 A 上 txnVolume 的总和?谢谢你们的帮助!干杯!

4

5 回答 5

1
Select sum(coalesce(txnVolume,0))
FROM A INNER JOIN B on A.BID = B.Bid
INNER JOIN C on C.ID = B.CID
WHERE C.CustID = 1 and C.ProdID = 1
于 2013-02-20T13:10:21.717 回答
1

尝试以下操作:

select sum(txnVolume) from A
   join B on A.bID = B.id
   join C on C.cID = C.id
  where prodID = 1 and custID = 1
于 2013-02-20T13:10:38.320 回答
1

在 sql 中,您需要连接表。当表连接时,它们会创建一个大表。然后,您可以将 where 子句添加到列中。

SELECT sum(txnVolume)
FROM A
JOIN B on A.bId = B.id
JOIN C on B.cID = c.id
where c.custid = 1 and c.prodid = 1
于 2013-02-20T13:10:42.607 回答
1
SELECT SUM(a.txnVolume)
FROM c 
  LEFT JOIN b 
    ON c.id=b.cId 
  LEFT JOIN a 
    ON b.id=a.bId 
WHERE c.cusId=1 AND c.prodId=1;
于 2013-02-20T13:11:11.003 回答
1
SELECT  SUM(a.txnVolume) totals
FROM    tableA a
        INNER JOIN tablB b
            ON a.bid = b.id
        INNER JOIN tableC c
            ON b.cid = c.ID
WHERE   c.custID = 1 AND c.prodID = 1
于 2013-02-20T13:11:33.697 回答