我写了下面的查询
def result=GuruActivity.executeQuery("select P.id, P.postBy, P.totalUp from GuruActivity as P where P.totalUp>5")
println result
render "done"
我得到的结果是这样的
[1, AAA, 7], [2, AAA, 10], [3, AAA, 7], [7, BBB, 7], [9, CCC, 7]
现在如何以下列格式(在 map 中)获取 P.ID,我在这里获取超过 5 totalUp 的 id
AAA : [1,2,3]
BBB : [7]
CCC : [9]
我该怎么做?
最佳答案
一种方法是使用集合的 groupBy
。
def list = [[1, 'AAA', 7], [2, 'AAA', 10],
[3, 'AAA', 7], [7, 'BBB', 7], [9, 'CCC', 7]]
assert list.groupBy{it[1]}
.collectEntries{k, v-> [k, v.collect{it[0]}]} ==
['AAA':[1, 2, 3], 'BBB':[7], 'CCC':[9]]
关于mysql - 用于 sql 的 Groovy groupby 或映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20016770/