如何 select 只有不在另一个 table 中的行而不获取另一个 table 的数据 - PLSQL

How to select only rows that are not in another table without getting data of the other table - PLSQL

我正在尝试 SELECT 具有特定条件的 table 中的所有行。我正在使用 Oracle PLSQL。

我有两个 table:

书籍

页数

一本书可以有多个页面。 我想找到所有没有任何页数的 50 字的书。

我找到的唯一解决方案是进行左外连接,然后按空值进行过滤。

Select * from Books b 
LEFT OUTER JOIN Page p on b.id = p.book_id and p.words = 50
where p.words is null

我认为这不是一个好的解决方案,但它确实有效,您知道其他方法吗?

我不想获取有关 Pages 的任何信息。如果我进行正常连接,然后应用条件,我会为一本书获得 N 行,而我只想为每本书获得 1 行

谢谢。

您可以调整您的 select,使其仅为 b.*

可能更常见的替代方法是 not exists:

select b.*
from Books b 
where not exists (select 1
                  from Page p 
                  where b.id = p.book_id and p.words = 50
                 );

这两种方法应该具有相似的性能特征。