簡體   English   中英

JOOQ:select 其他表中不存在 ID 的所有表條目

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM