【发布时间】: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);
【问题讨论】:
-
您的表名为
Subscriptions,而不是Subscribers。 You would not have run into this problem if you had used code generation,我强烈推荐!