假设我有这个GORM对象
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 "+
")"
)
}
希望这会有所帮助。这将返回一个列表列表,因为每个国家都有一个年龄最大的人群的列表。
关于grails - 使用条件,如何在Grails GORM中基于max(column)获取结果列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12607386/