2011-02-08 27 views
0

从使用JSP的SQL中检索数据时遇到问题。在使用组时,在JSP中获取聚合列值 -

ResultSet rs = statement.executeQuery("select orderdate, 
               SUM(orderingcost) 
             from `shopping`.`order` 
            group by orderdate"); 
int count = 0; 
//String 

while (rs.next()) { 
    //String orderdate = rs.getString("orderdate"); 
    //String orderingcost = rs.getString("orderingcost"); 
    System.out.println(count); 
    count++; 
} 

我使用group by语句来组合一些数据。

但是,我不知道如何在应用group by时获取数据。

任何人都可以帮助我吗?

回答

4

您需要别名柱:

select orderdate, SUM(orderingcost) orderingcost from `shopping`.`order` group by orderdate 

然后你就会有一个在你的结果集名为orderingcost列,你可以这样做:

double orderingcost = rs.getFloat("orderingcost") // probably need getFloat instead of string since it's a numeric value 
+1

+1:打我吧 – 2011-02-08 15:51:05