【问题标题】:Using Criteria, how do I get a list of result base on the max(column) in Grails GORM使用标准,我如何获得基于 Grails GORM 中的 max(column) 的结果列表
【发布时间】:2012-09-18 09:54:36
【问题描述】:

假设我有这个 GORM 对象

Class Person {
  String country
  int age
  String Name
}

我想为每个国家/地区找到最年长的人

我可以通过这种方式获得投影,但我想要 Person 对象的列表。

def results = Person.withCriteria {
  projections {
    groupProperty("country")
    max('age')
 }
}

【问题讨论】:

    标签: grails hql grails-orm criteria


    【解决方案1】:
    def countries = Person.executeQuery(
        "select distinct person.country from Person person")
    def persons = countries.collect{ country ->
        Person.executeQuery(
            "from Person as person1 " +
            "where person1.country='${country}' and person1.age=" +
                "("+
                "select max(age) "+
                "from Person as person2 "+
                "where person2.country='${country}' "+
                "group by person2.country "+
                ")"
             )
    }
    

    希望这会有所帮助。这将返回一个列表列表,因为每个国家/地区都有可能存在一个具有最大年龄的人的列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 2011-01-26
      • 2015-10-23
      相关资源
      最近更新 更多