以下是代码:
SelectQuery<Record> resultQuery = dslContext.selectQuery();
for(int floor : floors) {
method1(resultQuery,floor);
}
然后:
method1(SelectQuery<Record> resultQuery,int floor){
SelectQuery<Record> query = dslContext.selectQuery();
/*
Now I am creating subqueries for parameter floor by adding condition
in above created query
*/
resultQuery.union(query);
}
问题是在最终查询(resultQuery)中我得到了&#34;选择*来自双重&#34;追加。它正在添加,因为我创建了&#34; resultQuery&#34;空的。
我有什么方法可以替换或删除&#34;选择*来自双&#34;来自最终查询。
由于
答案 0 :(得分:0)
SelectQuery<Record> result = null;
for (int floor : floors) {
result = method1(result, floor);
}
使用:
SelectQuery<Record> method1(SelectQuery<Record> result, int floor) {
SelectQuery<Record> query = dslContext.selectQuery();
/* your logic ... */
if (result == null)
return query;
else
return result.union(query);
}
Optional<Select<Record>> result =
floors.stream()
.map(this::method2)
.reduce(Select::union)
使用:
Select<Record> method2(int floor) {
SelectQuery<Record> query = dslContext.selectQuery();
/* your logic ... */
return query;
}
另见: