简体   繁体   English

在jOOQ的SQL查询中结合普通SQL和DSL

[英]Combine plain SQL and DSL in SQL query in jOOQ

I have a complex plain SQL query (with subselects, multiple joins, database specific functions) but I would like to use the jOOQ's DSL for generating eg the order by clause. 我有一个复杂的普通SQL查询(具有子选择,多个联接,特定于数据库的功能),但我想使用jOOQ的DSL生成例如order by子句。

What I would like to achieve is: 我想要实现的是:

DSL
    .using(datasource)
    .select("select column from table")
    .orderBy(DSL.fieldByName("column"))

which jOOQ could be transforming to: jOOQ可能会转换为:

select * (select column from table) q order by q.column;

Can this be done? 能做到吗?

You're close. 你近了 The following is possible: 以下是可能的:

DSL.using(datasource, dialect)
   .select()
   .from("(select column from table) t")
   .orderBy(DSL.field("t.column"));

You have to wrap (and depending on the SQL dialect, also rename) your derived table explicitly yourself. 您必须自己明确地包装(并根据SQL方言,还要重命名)派生表。

Note that I'm using DSL.field() , not DSL.fieldByName() , as the latter produces a case-sensitive column reference. 请注意,我使用的是DSL.field()而不是DSL.fieldByName() ,因为后者会产生区分大小写的column引用。 Your plain SQL query would then also have to produce a case-sensitive column reference. 然后,您的普通SQL查询还必须产生区分大小写的column引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM