简体   繁体   中英

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

+-------------------+   +----------------------+
|  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. Should be: 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM