1

如果在表“b”的两个字段上未找到匹配项,我试图仅从表“a”中选择记录。我想出了以下内容,但它只是返回所有记录。代码看起来很简单,是 Access 没有正确处理 SELECT 1 函数还是我没有正确使用它?感谢您的任何帮助。

SELECT a.[Part Number], 
       a.[Company Name], 
       a.[Customer Name], 
       a.[Customer Number], 
       a.[Salesperson Initials], 
       a.[Date Received], 
       a.Quantity, 
       a.ExtCost, 
       a.ExtPrice, 
       a.[Gross Margin]
FROM   [1: Orders in the period] As a
WHERE EXISTS (SELECT 1 
              FROM [Price Adjustments New Gross Margin] As b 
              WHERE b.[Part Number]<>a.[Part Number] AND b.[Customer Name]<>a.[Customer Name]);
4

1 回答 1

1

LEFT JOIN ato并仅返回sideb所在的那些行。bIs Null

SELECT a.[Part Number], 
       a.[Company Name], 
       a.[Customer Name], 
       a.[Customer Number], 
       a.[Salesperson Initials], 
       a.[Date Received], 
       a.Quantity, 
       a.ExtCost, 
       a.ExtPrice, 
       a.[Gross Margin]
FROM
    [1: Orders in the period] AS a
    LEFT JOIN [Price Adjustments New Gross Margin] AS b 
    ON
            a.[Part Number]=b.[Part Number]
        AND a.[Customer Name]=b.[Customer Name]
WHERE b.[Part Number] Is Null;

如果您更喜欢坚持使用EXISTS,我认为这可能会得到您想要的。

WHERE NOT EXISTS
    (
        SELECT 1 
        FROM [Price Adjustments New Gross Margin] AS b 
        WHERE
                b.[Part Number]=a.[Part Number]
            AND b.[Customer Name]=a.[Customer Name]
    );
于 2013-07-19T17:49:20.290 回答