如何在Grails中实现“select sum()”

时间:2010-04-26 02:51:03

标签: grails hql gorm

我有这样的关系:

class E {
  string name
  hasMany = [me:ME]
}

class M {
  float price
}

class ME {
 E e
 M m
 int quantity
}

而且我讨厌迭代来实现这个目标:“获得ME中数量的总和乘以给定E的相应M的价格。”

有关如何使用GORM / HQL实现它的任何提示?

提前致谢

1 个答案:

答案 0 :(得分:8)

没有对此进行过测试,但如果它没有开箱即用,它应该关闭:

def givenE = // the E we're looking for
def sum = ME.executeQuery("select sum(m.price) from ME as me join me.m as m where me.e = :e", [e: givenE])
println sum