访问 SQL 查询:Return 值不在相关表的间隔之间

Access SQL Query: Return values not between an interval of related tables

我有 2 个 tables(间隔和深度)与名称字段相关。我希望查询 return 所有不在区间 table 中的每个名称的深度(或不等于或介于顶部和底部之间)。当 Intervals table 中有多个 Name 记录时,我的查询失败(例如:Name 字段中有 2 'one' 条记录)。

间隔

Name    Top    Bottom
one     2      3
one     5      7
two     2      3
three   3      4

深度

Name    Depth
one     1
one     2
one     3
one     4
one     5
one     6
one     7
one     8
two     1
two     2
two     3
two     4
two     5
three   1
three   2
three   3
three   4
three   5

我的查询:

SELECT Intervals.Name, Depths.Depth
FROM Depths INNER JOIN Intervals ON Depths.Name = Intervals.Name
WHERE (((Depths.Depth) < [Intervals]![Top] 
    Or (Depths.Depth) > [Intervals]![Bottom]))
ORDER BY Intervals.Name, Depths.Depth;

我知道这会失败,因为 Where 子句单独应用于间隔中的每个名称记录。 Where 子句应适用于按名称相关的所有间隔记录,因此结果不包括间隔 table.

中的任何从上到下的间隔

我的查询输出:

Name    Depth
one     1
one     1   
one     2    
one     3
one     4    
one     4
one     5
one     6
one     7
one     8   
one     8
three   1
three   2
three   5
two     1
two     4
two     5

期望的输出:所有深度不在间隔中

Name    Depth   
one     1       
one     4       
one     8       
two     1
two     4
two     5
three   1
three   2
three   5

您问题的措辞表明 not exists,因此这可能对您有用:

select d.*
from depths as d
where not exists (select 1
                  from intervals as i
                  where i.name = d.name and d.depth between i.[top] and i.[bottom]
                 );