需要下表的Mysql查询

时间:2018-01-30 06:17:17

标签: c# mysql

我有3张桌子

表1

---------------------------
Cid | Name
-----------------------------
C1 |  Customer 1
------------------------------
C2 |  Customer2
------------------------------
C3 |  Customer3

表2

---------------------------
Cid | ItemId
-----------------------------
C1 |  i1
------------------------------
C2 |  i1
-----------------------------
C1 |  i2
------------------------------
C2 |  i3
-----------------------------
C1 |  i4
------------------------------
C2 |  i4
-----------------------------
C1 |  i5
------------------------------

表3

---------------------------
ItemId | price
-----------------------------
I1  |  10
------------------------------
I2  |  14
------------------------------
I3  |  15
------------------------------
I4  |  16
------------------------------
I5  |  20
------------------------------

需要一个 选择查询以获得结果

Cid | CustomerName | totalprice
----------------------------------------------------
C1 | Customer 1 | 60
----------------------------------------------------
C2 | Customer 2 | 31
----------------------------------------------------
C3 | Customer 3 | 0

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT t1.CID,t1.NAME,SUM(t3.prize)
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.CID = t2.CID
INNER JOIN on Table3 t3 ON t2.ItemID = t3.ItemID
GROUP BY t1.CID,t1.NAME