删除"从双"中选择*来自JOOQ的UNION

时间:2018-03-15 07:33:30

标签: java sql union jooq

以下是代码:

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;来自最终查询。

由于

1 个答案:

答案 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;
}

另见: