【问题标题】:JOOQ : select all table entries where ID not present in other tableJOOQ:选择其他表中不存在 ID 的所有表条目
【发布时间】:2022-01-11 05:27:56
【问题描述】:

问题:你如何创建一个 JOOQ 查询到 select 所有 Subscription 条目 where ActiveSubscribers.subscriptionId 不在 Subscriptions 表中

+-------------------+   +----------------------+
|  Subscriptions    |   |  ActiveSubscribers   |
+-------------------+   +----------------------+
| Id   |    Name    |   | Id   | SubcriptionId |
|-------------------|   |----------------------|
| 1    |  Dogs      |   | 1    |   1           |
| 2    |  Cats      |   | 2    |   2           |
| 3    |  Hamsters  |   +----------------------+
+-------------------+

预期结果:

+-------------------+
| 3    |  Hamsters  |
+-------------------+

我的尝试:

List<Subscription> nonActiveSubscriptions = DSL.using(connection)
.select()
.from(DSL.table("Subscribers"))
.where(DSL.field("Id").notIn(
  DSL.select(DSL.field("ActiveSubscribers.subscriptionId")).from(DSL.table("Subscribers"))
)
.fetch()
.into(Subscription.class);

【问题讨论】:

标签: java sql jooq


【解决方案1】:

您的子选择的 from 子句是错误的。应该是:ActiveSubscribers

List<Subscription> nonActiveSubscriptions = DSL.using(connection)
.select()
.from(DSL.table("Subscribers"))
.where(DSL.field("Id").notIn(
  DSL.select(DSL.field("ActiveSubscribers.subscriptionId"))
     .from(DSL.table("ActiveSubscribers"))
)
.fetch()
.into(Subscription.class);

【讨论】:

    猜你喜欢
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多