Playframework 如何对Play Framework模型中的字段求和
在尝试学习Play Framework时,我遇到了以下问题: 如何计算Play框架模型中字段的摘要?我想做与SQLs“SELECT SUM(totalAmount)FROM events where employee_id=23”等效的事情 我可以自己计算摘要,就像下面的代码一样,但我更希望SQL server进行计算:Playframework 如何对Play Framework模型中的字段求和,playframework,Playframework,在尝试学习Play Framework时,我遇到了以下问题: 如何计算Play框架模型中字段的摘要?我想做与SQLs“SELECT SUM(totalAmount)FROM events where employee_id=23”等效的事情 我可以自己计算摘要,就像下面的代码一样,但我更希望SQL server进行计算: public static Double countSaldoForEmployee(Employee e) { // Get all Events for the e
public static Double countSaldoForEmployee(Employee e) {
// Get all Events for the employee
List<Event> events = Event.find("byEmployee", e).fetch();
Double sum = 0.0 ;
for(Event event: events) {
if(event.totalAmount != null) {
sum += event.totalAmount;
}
}
return sum;
}
public静态双计数saldoForemployee(员工e){
//获取员工的所有事件
List events=Event.find(“byEmployee”,e).fetch();
双和=0.0;
对于(事件:事件){
如果(event.totalAmount!=null){
总和+=事件总数;
}
}
回报金额;
}
您应该能够通过一个简单的本机查询来实现这一点。类似下面的内容
public static Double countSaldoForEmployee(Employee e) {
return (Double)em.createNativeQuery("SELECT SUM(e.totalAmount) FROM events e where e.employee_id = "+e.id).getSingleResult();
}
非常感谢。这正是我想要的。