【问题标题】:How to implement "select sum()" in Grails如何在 Grails 中实现“select sum()”
【发布时间】:2010-04-26 02:51:03
【问题描述】:

我有这样的关系:

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

class M {
  float price
}

class ME {
 E e
 M m
 int quantity
}

我不想通过迭代来实现这一点:“获得 ME 中的数量之和乘以给定 E 的相应 M 的价格”。

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

提前致谢

【问题讨论】:

    标签: grails hql grails-orm


    【解决方案1】:

    尚未对此进行测试,但如果它不能开箱即用,它应该是关闭的:

    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
    

    【讨论】:

    • 这非常接近,但executeQuery() 实际上返回一个列表/数组,其中总和作为第一个元素。所以它是def sum = ME.executeQuery("...")[0]
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    相关资源
    最近更新 更多