JOOQ:来自 table 的 Select 键不存在于另一个 table 中

JOOQ: Select keys from a table not present in another table

如何在 jooq 中获取一个 table 中另一个 table 中不存在的所有键?或者什么是等同于以下 SQL 命令:

SELECT ID FROM A WHERE ID NOT IN (SELECT ID FROM B)

试试这个:

SELECT ID FROM A WHERE not exists (SELECT ID FROM B where B.ID = A.ID )
Select id from A
   Minus
Select id from B; -- all from A where not in B

Select id from B
  Minus
Select id from A; -- all from B where not in A

这将是 1:1 jOOQ 的翻译:

DSL.using(configuration)
   .select(A.ID)
   .from(A)
   .where(A.ID.notIn(select(B.ID).from(B)))
   .fetch();

以上假设静态导入是这样的:

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

使用注意事项NOT IN

SQL NOT IN 谓词只有在子查询不产生任何 NULL 值时才能正常工作。 In the presence of a single NULL value, the entire predicate will not return any rows.

通常使用 NOT EXISTS 更好。或者在 jOOQ 中:

DSL.using(configuration)
   .select(A.ID)
   .from(A)
   .where(notExists(selectOne().from(B).where(B.ID.eq(A.ID))))
   .fetch();

在这种情况下最好使用左连接,它只是一个查询

SELECT A.ID FROM A LEFT JOIN B ON A.ID=B.ID WHERE B.ID IS NULL;