【发布时间】:2014-04-20 15:37:15
【问题描述】:
应该很简单,但我目前真的很困惑
我有一个数据库查询:
public double markingAvg(Marking id) {
System.out.println("In MarkingAvg");
System.out.println("id = " + id);
System.out.println("id = " + id.getId());
Query m = em.createQuery("SELECT m.markSectionOne, m.markSectionTwo, m.markSectionThree, m.markSectionFour, m.markSectionFive, m.markSectionSix, m.markSectionSeven, m.markSectionEight, m.markSectionNine, m.markSectionTen, m.markSectionEleven, m.markSectionTwelve, m.markSectionThirteen FROM MARKING m WHERE m.id = :id", Double.class);
m.setParameter("id", id.getId()); // Note the getId()
System.out.println(m);
List<Object[]> allMarks = m.getResultList();
double total = 0.0;
int count = 0;
for (Object[] marks : allMarks) {
for (Object mark : marks) {
total += Double.parseDouble((String) mark);
++count;
}
}
return total / (double) count;
}
它只是从 13 列中获取值并对它们进行平均,但是当它们中有空值时它会中断,有没有办法说是否有任何列中的空值要返回总数为0?谢谢
【问题讨论】:
-
您的意思是,如果列的值为 NULL,则应将其评估为 0,或者一旦其中一列为 NULL,您希望总数等于 0?
-
理想情况下,如果一列为 NULL,则应将其评估为 0,但是一旦单列为空,总计 = 0 也是可以接受的