在另一个具有不同条件的表中查找缺失的日期

问题描述 投票:0回答:1

我有多个易失性表,其结构如下

表1

Ref#     criteria.   Date
abc123.   Yellow.    10/02/1990
abc123.   Yellow.    11/02/1990
abc123.   Yellow.    08/02/1990

表2

Ref#.    Criteria.   Date
abc123.   Pink.      10/02/1990
abc123.   Pink.      11/02/1990
abc123.   Pink.      08/02/1990
abc123.   Pink.      05/02/1990

我想编写一个与 ref# 匹配的查询,但从其他表中找到不同条件下另一个表中不存在的日期,例如黄色与粉色

我尝试了一个查询,在其中执行了左连接和内连接 A.Ref#=b.ref# 和 a.date<>b.date

但它只是找到不匹配的行。我尝试过也不存在,但这也没有达到我想要的效果。

我只想从引用#仍然匹配的其他表中取出不存在的单个日期

date join not-exists
1个回答
0
投票

使用左连接:

SELECT 
    [Table 2].[Ref#], 
    [Table 2].Date
FROM 
    [Table 2] 
LEFT JOIN 
    [Table 1] 
    ON ([Table 2].Date = [Table 1].Date) AND ([Table 2].[Ref#] = [Table 1].[Ref#])
WHERE 
    [Table 1].Date Is Null

结果:

© www.soinside.com 2019 - 2024. All rights reserved.