如何获取一个表中jooq中另一个表中不存在的所有键?或者什么等同于以下SQL命令:
SELECT ID FROM A WHERE ID NOT IN (SELECT ID FROM B)
答案 0 :(得分:2)
这将是对jOOQ的1:1翻译:
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
当子查询不产生任何NOT IN
值时,SQL NULL
谓词才能正常工作。 In the presence of a single NULL
value, the entire predicate will not return any rows
通常最好使用NOT EXISTS
as shown in this answer。或者在jOOQ:
DSL.using(configuration)
.select(A.ID)
.from(A)
.where(notExists(selectOne().from(B).where(B.ID.eq(A.ID))))
.fetch();
答案 1 :(得分:0)
试试这个:
SELECT ID FROM A WHERE not exists (SELECT ID FROM B where B.ID = A.ID )
答案 2 :(得分:0)
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
答案 3 :(得分:0)
在这种情况下,最好使用左联接,它只是一个查询
SELECT A.ID FROM A LEFT JOIN B ON A.ID=B.ID WHERE B.ID IS NULL;