我有一个返回结果的查询:
Table Units Table Payments
ID | Payment
1003 | 300.00
1003 | 100.00
1004 | 350.00
1005 | 200.00
如何构建查询,使其组合相同的ID,如下所示:
ID | Payment
1003 | 400.00
1004 | 350.00
1005 | 200.00
我的查询如下:
SELECT u.id, p.amt FROM units u JOIN payments p ON u.id = p.unit
我已尝试SUM()
amt
字段,但它只返回一个结果:
ID | Payment
1003 | 950.00
答案 0 :(得分:6)
SELECT u.id, sum(p.amt) as payment
FROM units u inner JOIN payments p ON u.id = p.unit
group by u.id
<强> DEMO 强>