简体   繁体   English

我怎样才能让 JooQ 联合两个不同的表,它们具有相同的列和数据类型?

[英]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.

相关问题 当结果记录有两列具有相同名称的不同表时,如何在 jooq 中按名称选择列? - How can I select a column by name in jooq, when result record has two columns of different tables with the same name? 我如何创建两个具有相同Columnmodel但具有不同模型的Jtables - How can i create two Jtables which have same Columnmodel, but they have different models 如何使用具有不同命名空间和相同JAXB类的两个不同端点? - How can I have two different endpoint with different namespace and same JAXB class? jooq getValue(String fieldName)不同表中具有相同名称的列 - jooq getValue(String fieldName) Columns with the same name in different tables Maven:如何在两个不同的模块下有一个同名的模块? - Maven: How can I have a module with same name under two different modules? 购买两个相同但组件顺序不同的混合数字时,如何返回 true? - How can I return true when buying two mixed figures that are the same but have the components in a different order? 如何在JOOQ中使用@variable? - How can we have @variable in JOOQ? 如何为同一个 CustomInfoWindow 设置不同的事件 - How can I have different events for the same CustomInfoWindow 如何从不同的类访问同一个对象 - How can I have access to the same object from different classes 如何让两个类共享相同的变量定义 - How can I have two classes share the same variable definitions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM