这是我的桌子:
表名:玩家。所有数据类型均为浮点型
id | pts | gp
0 | 45 | 5
1 | 13 | 6
2 | 74 | 10
我想选择平均分。这是我连接到数据库后单击按钮时使用的功能。
public float getavgstats() {
float ptsavg = 0;
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select (cast((pts/gp) as decimal(10,2))) as ppg from player order by ppg desc");
while (rs.next()) {
ptsavg = rs.getFloat("pts");
System.out.println(ptsavg);
}
} catch (Exception ex) {
System.out.println("error" + ex);
}
return ptsavg;
}
}
我收到一条错误消息,即找不到“ pts”列
最佳答案
您的查询仅返回一列-ppg
,其中没有pts
列。
如果要查询,则应将其添加到查询中:
ResultSet rs = stmt.executeQuery(
"select pts, (cast((pts/gp) as decimal(10,2))) as ppg " +
// Here-^
"from player order by ppg desc");