简体   繁体   English

如何使用LIMIT子句生成SQL语句但在jOOQ中没有OFFSET子句?

[英]How to generate SQL statement with LIMIT clause but without OFFSET clause in jOOQ?

I'm trying to use jOOQ to generate the following SQL statement: 我正在尝试使用jOOQ生成以下SQL语句:

SELECT id, name
FROM students
ORDER BY id DESC
LIMIT 50;

To generate the above statement with jOOQ: 使用jOOQ生成上述语句:

String sql = DSL.using(SQLDialect.POSTGRES).select(
  field("id"),
  field("name"))
  .from("students")
  .orderBy(field("id").desc())
  .limit(inline(50))
  .getSQL();

But I get the following: 但我得到以下内容:

select id, name from students order by id desc limit 50 offset ?

How do I remove the OFFSET clause? 如何删除OFFSET子句? I know that I can specify the offset value to 0, which is same as omitting the OFFSET clause, but I want to know whether I can completely remove it from the generated SQL statement. 我知道我可以将偏移值指定为0,这与省略OFFSET子句相同,但我想知道是否可以从生成的SQL语句中完全删除它。

Thank you. 谢谢。

You're right, jOOQ 3.4.2 currently renders an OFFSET clause for PostgreSQL, regardless of whether it was specified by users through the API or not. 你是对的,jOOQ 3.4.2当前为PostgreSQL呈现OFFSET子句,无论用户是否通过API指定它。 That could probably be improved. 这可能会有所改善。 I've created issue #3577 for this. 我为此创建了问题#3577

Currently, there isn't really an "easy" way to change the generated SQL in this case. 目前,在这种情况下,实际上没有一种“简单”的方法来更改生成的SQL。 You could be implementing an ExecuteListener , and patch the generated SQL - but you'd also have to get rid of the bind value. 您可能正在实现ExecuteListener ,并修补生成的SQL - 但您还必须摆脱绑定值。

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

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