我试图构造一个查询来查找PostgreSQL表(a
)中的所有记录,
b
中根本没有 条记录,其中有state='ready'
在表b
中没有记录的状态为state='ready'
的原因没关系(可以是没有关联,关联为null,状态不同或状态为空)
在下面的示例中,我希望在表2,3,4
中找到ID为a
的记录。
我尝试了左联接,但无法使其正常工作。
PS。该查询必须高效,因为表具有数百万条记录。
答案 0 :(得分:0)
想必,您只想要not exists
:
select a.*
from a
where not exists (select 1
from b
where b.? = a.? and -- whatever the join conditions are
b.state = 'ready'
);
答案 1 :(得分:0)
尝试一下:
SELECT * FROM a WHERE id NOT IN
(SELECT b.a_id FROM b WHERE b.state = 'ready');