如果ALL记录与另一个表中的某些内容相等,则尝试显示记录

时间:2012-05-10 06:16:26

标签: sql ms-access

我正在使用3个表格,Part,PartDrawing和ShopDrawing。基本上我想显示其所有零件都有库存的所有ShopDrawings。但是,每个ShopDrawing都有多个PartDrawings,所以当我运行我的查询时:

SELECT DISTINCT ShopDrawing.DrawingNo FROM ShopDrawing, PartDrawing, Part
WHERE ShopDrawing.DrawingNo = PartDrawing.DrawingNo
AND PartDrawing.PartNo = Part.PartNo
AND Part.InStock = 'YES';

它显示每一个ShopDrawing,因为它们都至少有一个库存的零件。我只想要它显示ShopDrawings,其中所需的所有零件都有库存,我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

尝试评估时不选择Part.InStock = 'NO'的数据,而不是选择Part.InStock = 'YES'的数据

SELECT 
     ShopDrawing.DrawingNo 
FROM 
     ShopDrawing WHERE ShopDrawing.DrawingNo NOT IN
         (SELECT 
              PartDrawing.DrawingNo 
          FROM 
              PartDrawing LEFT JOIN Part ON PartDrawing.PartNo = Part.PartNo 
          WHERE 
              Part.InStock IS NULL
          GROUP BY PartDrawing.DrawingNo 
          HAVING COUNT(*)>0)