假设我有这个GORM对象
Class Person {
String country
int age
String Name
}
我想为每个国家找到最年长的人
我可以通过这种方式获得投影,但我希望获得Person对象列表。
def results = Person.withCriteria {
projections {
groupProperty("country")
max('age')
}
}
答案 0 :(得分:0)
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 "+
")"
)
}
希望这会有所帮助。这将返回一个列表列表,因为每个国家都有可能列出具有最大年龄的人员。