[英]JOOQ : select all table entries where ID not present in other table
Question: How do you create a JOOQ query to select
all Subscription
entries where
ActiveSubscribers.subscriptionId
not in Subscriptions
table问题:如何创建对
select
所有Subscription
条目的 JOOQ 查询, where
ActiveSubscribers.subscriptionId
不在Subscriptions
表中
+-------------------+ +----------------------+
| Subscriptions | | ActiveSubscribers |
+-------------------+ +----------------------+
| Id | Name | | Id | SubcriptionId |
|-------------------| |----------------------|
| 1 | Dogs | | 1 | 1 |
| 2 | Cats | | 2 | 2 |
| 3 | Hamsters | +----------------------+
+-------------------+
Expected result:预期结果:
+-------------------+
| 3 | Hamsters |
+-------------------+
What I've tried:我试过的:
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);
The from clause of your subselect is wrong.您的子选择的 from 子句是错误的。 Should be: ActiveSubscribers
应该是: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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.