【问题标题】:How can I execute the command in JOOQ with double select如何使用双选在 JOOQ 中执行命令
【发布时间】:2022-01-19 13:09:32
【问题描述】:

我正在使用代码生成器!

我需要执行以下查询:

SELECT sbcm_ref.process_time_reports_direct_buf_record((SELECT tr.time_reports_buf_id
FROM sbcm_buf.time_reports_direct_buf tr ORDERBY time_reports_buf_id desc limit 1

这是我的命令

DSL.using(connection).select(Routines.processTimeReportsDirectBufRecord(select(TIME_REPORTS_DIRECT_BUF.TIME_REPORTS_BUF_ID).from(TIME_REPORTS_DIRECT_BUF).orderBy(TIME_REPORTS_DIRECT_BUF.TIME_REPORTS_BUF_ID.desc()).limit(1).fetch())).fetch();

哪里出错了?

谢谢!

【问题讨论】:

    标签: jooq


    【解决方案1】:

    您可以将任何Select<R extends Record1<T>> 转换为Field<T>,方法是用DSL.field(Select) 包裹它:

    ctx.select(Routines.processTimeReportsDirectBufRecord(field(
        select(TIME_REPORTS_DIRECT_BUF.TIME_REPORTS_BUF_ID)
        .from(TIME_REPORTS_DIRECT_BUF)
        .orderBy(TIME_REPORTS_DIRECT_BUF.TIME_REPORTS_BUF_ID.desc())
        .limit(1)
    ))).fetch();
    

    See also the manual section about scalar subqueries。但是,在这种特殊情况下,您不需要它。为什么不直接写:

    ctx.select(Routines.processTimeReportsDirectBufRecord(
            max(TIME_REPORTS_DIRECT_BUF.TIME_REPORTS_BUF_ID)
        ))
       .from(TIME_REPORTS_DIRECT_BUF)
       .fetch();
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 2016-09-04
      • 1970-01-01
      • 2016-04-09
      • 2011-07-22
      • 2021-04-26
      • 2018-04-21
      • 2020-09-18
      • 1970-01-01
      相关资源
      最近更新 更多