[英]How to execute with Jooq arbitrary number of selects joined with UNION
我想執行List<Select>
UNION
SELECT #1
UNION
SELECT #2
UNION
...
如何使用Jooq?
更新:在我的方法中,我檢查了一組參數,如果它們不是nul,則將子查詢添加到列表中。 我需要像字符串連接器這樣的功能。
所以我通過降級到原始sql並使用Joiner(" UNION ").join(List<String>subqueries)
解決了我的任務
也許可以將以下內容應用於在多個Select
上調用union
。
<T extends Record> Result<T> fetchUnion(List<Select<T>> selects) {
return selects.stream()
.reduce(Select::union)
.map(Select::fetch)
.orElseThrow(() -> /* your exception? */);
}
請注意,我尚未對其進行測試,您可能需要對其進行一些改動才能使其正常運行。 或者:
<T extends Record> Result<T> fetchUnion(Select ... selects) {
return Stream.of(selects)
.reduce(Select::union)
.map(Select::fetch)
.orElse ... ;
}
可能的呼叫可能是:
Result result = fetchUnion(select(T1.A, T1.B).from(T1),
select(T2.A, T2.B).from(T2),
select(T3.A, T3.B).from(T3));
這里還需要Lukas提到的靜態導入:
import static org.jooq.impl.DSL.*;
干得好:
using(configuration)
.select(T1.A, T1.B)
.from(T1)
.union(
select(T2.A, T2.B)
.from(T2))
.union(
select(T3.A, T3.B)
.from(T3))
.fetch();
上面的答案假設以下導入:
import static org.jooq.impl.DSL.*;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.