如何使用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]这样的单个数组中时,如何获得汇总结果?
提前致谢!
答案 0 :(得分:4)
您还没有真正提供足够的信息。目前尚不清楚如何将这些查询发送到数据库,这可能最终具有相关性。如果你真的需要单独发送它们,那么就把它和结果结合起来......
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
}