使用Criteria,如何在Grails GORM中获得基于max(列)的结果列表
假设我有这个GORM对象使用Criteria,如何在Grails GORM中获得基于max(列)的结果列表,grails,hql,gorm,criteria,Grails,Hql,Gorm,Criteria,假设我有这个GORM对象 Class Person { String country int age String Name } 我想得到每个国家最年长的人 我可以这样得到投影,但我想得到Person对象的列表 def results = Person.withCriteria { projections { groupProperty("country") max('age') } } 希望这能有所帮助。这将返回一份名单,因为每个国家都可能有一份年龄最大的
Class Person {
String country
int age
String Name
}
我想得到每个国家最年长的人
我可以这样得到投影,但我想得到Person对象的列表
def results = Person.withCriteria {
projections {
groupProperty("country")
max('age')
}
}
希望这能有所帮助。这将返回一份名单,因为每个国家都可能有一份年龄最大的人的名单
希望这能有所帮助。这将返回一份名单,因为每个国家都可能有一份年龄最大的人的名单
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 "+
")"
)
}