使用jooq删除子查询

时间:2017-02-20 16:46:00

标签: java sql jooq

我想使用jooq来处理这条SQL指令(listName是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 )

我写了这个提议:

    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();

这是一种正确的方法吗?

1 个答案:

答案 0 :(得分:1)

您的对帐单中没有涉及批量查询。此外,我不确定您为什么需要加入子查询中的calendar表。这是另一种SQL查询:

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

转换为以下jOOQ查询

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

以上jOOQ查询(一如既往)假设您静态导入此内容:

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