计算每个客户的总付款额

问题描述 投票:0回答:1

Here's the database structure

我想使用“金额”列和“客户编号”列总计每个客户已花费的金额。

    String query = "SELECT customerNumber, SUM(amount) FROM payments";
    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()) {
        orderNumbers.add(rs.getDouble(1));
        System.out.println(rs.getDouble(1));
    }

我已经在上面尝试过此代码,并且它只返回第一个customerNumber。

java mysql sql jdbc
1个回答
0
投票

您需要使用group by子句进行聚合:

SELECT customerNumber, SUM(amount) 
FROM payments 
GROUP BY customerNumber;
© www.soinside.com 2019 - 2024. All rights reserved.