简体   繁体   中英

Delete with subquery using jooq

I would like to use jooq to process this SQL instruction (listName is a List < String >) :

DELETE FROM table_calendar cal
WHERE cal.client_id
IN (
SELECT client.id FROM table_client client
JOIN cal 
ON cal.client_id = client.id
WHERE client.name = :listName )

I wrote this proposal :

    SelectConditionStep<Record1<String>> res = create.select(CALENDAR.CLIENT_ID)
                        .from(CALENDAR)
                        .join(CLIENT)
                        .on(CLIENT.ID.eq(CALENDAR.ID))
                        .where(CLIENT.NAME.in(listName));

    Query deleteQuery = create.delete(CALENDAR)
                    .where(CALENDAR.ID.in(res.getBindValues()));

    create.batch(deleteQuery).execute();

is it a correct approach?

There's no batch query involved in your statement. Also, I'm not sure why you need to join the calendar table in your subquery. Here's an alternative SQL query:

DELETE FROM calendar
WHERE client_id IN (
    SELECT client.id
    FROM client
    WHERE client.name = :listName
)

Which translates to the following jOOQ query

create.delete(CALENDAR)
      .where(CALENDAR.CLIENT_ID.in(
           select(CLIENT.ID)
          .from(CLIENT)
          .where(CLIENT.NAME.eq(listName))
      ))
      .execute();

The above jOOQ query (as always) assumes you're static importing this:

import static org.jooq.impl.DSL.*;

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