[英]JOOQ : select all table entries where ID not present in other table
問題:如何創建對select
所有Subscription
條目的 JOOQ 查詢, 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);
您的子選擇的 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.