gpt4 book ai didi

grails - 使用条件,如何在Grails GORM中基于max(column)获取结果列表

转载 作者:行者123 更新时间:2023-12-02 14:12:52 24 4
gpt4 key购买 nike

假设我有这个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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com