[英]How can I have JooQ union two different tables, that have the same columns and datatypes?
I have two tables in my database, these tables have the same exact columns and datatypes but in different order.我的数据库中有两个表,这些表具有完全相同的列和数据类型,但顺序不同。
Table A has the columns: A, B, C, D.表 A 有列:A、B、C、D。
Table B has the columns: A, C, D, B.表 B 有列:A、C、D、B。
I have an application that uses JooQ to query the database, and that it utilized the Codegen to create the Tables and Records.我有一个使用 JooQ 查询数据库的应用程序,它使用 Codegen 创建表和记录。 However, it will not allow me to union my two select statements due to a compile error.但是,由于编译错误,它不允许我合并我的两个 select 语句。 What can I do, to union the two tables?我能做些什么来联合这两个表?
StepWhereSelect<ARecord> query = dsl.selectFrom(A);
StepWhereSelect<BRecord> query2 = dsl.selectFrom(B);
query.union(query2)
On the variable inside the union function, it compains about a type mismatch from SelectUnionStep about union(ARecord) cannot be applied to union(BRecord).在 union 函数内部的变量上,它包含 SelectUnionStep 中关于 union(ARecord) 不能应用于 union(BRecord) 的类型不匹配。
Where can I do in JooQ to union these 2 tables?我在哪里可以在 JooQ 中合并这两个表?
What you're looking for is jOOQ support for the SQL standard syntax UNION CORRESPONDING
.您正在寻找的是 jOOQ 对 SQL 标准语法UNION CORRESPONDING
。 The syntax is currently not implemented in any RDBMS I'm aware of, but it could be emulated by jOOQ, easily.该语法目前没有在我所知道的任何 RDBMS 中实现,但它可以很容易地被 jOOQ 模拟。 In the absence of this syntax support, you can create the correct order of columns in both subqueries using this code:在没有这种语法支持的情况下,您可以使用以下代码在两个子查询中创建正确的列顺序:
var q1 = dsl.select(Stream.of(A.fields()).sorted(Field::getName).collect(toList()))
.from(A);
var q2 = dsl.select(Stream.of(B.fields()).sorted(Field::getName).collect(toList()))
.from(A);
q1.union(q2).fetch();
Of course, there is no type safety in this anymore.当然,这里不再有类型安全了。 If you still want to receive ARecord
types, you can write如果你还想接收ARecord
类型,你可以写
q1.union(q2).fetchInto(A);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.