如何使用Groovy将以下查询的结果存储在同一数组中?
select id from employee where employee_id=100
select id from employee where employee_id=200
select id from employee where employee_id=300
例:
def calculate(employee_ids)
{
def result
Sql sql = new Sql(dataSource)
for (employee_id in employee_ids) {
result = sql.rows("select id from employee where employee_id="+employee_id)
}
sql.close()
return result
}
现在,当我将100、200、300作为'employee_id'传递时,'calculate()'函数仅返回300。 (“employee_id”为数组形式)
将100、200、300等作为'employee_id'传递到单个数组(如[100,200,300])时,如何获得合计结果?
提前致谢!
最佳答案
您尚未真正提供足够的信息。尚不清楚您如何将这些查询发送到数据库,最终可能是相关的。如果您确实需要单独发送它们,则可以将结果与类似的内容组合在一起...
def firstResult = // send query to db...
def secondResult = // send query to db...
def thirdResult = // send query to db...
def combinedResults = firstResult + secondResult + thirdResult
但是也许您真正想要的是执行类似这样的查询...
select id from employee where employee_id in (100, 200, 300)
编辑:
你可以做这样的事情...
def result = []
for (employee_id in employee_ids) {
result += sql.rows("select id from employee where employee_id=$employee_id").id
}
关于grails - 使用Groovy/Grails将结果集存储在单个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930944/