MS Access错误:'此子查询最多可返回一条记录'

时间:2014-02-22 05:18:14

标签: sql ms-access

我尝试连接3个表但收到错误At most one record can be returned by this subquery

我的代码是

SELECT InvoiceNumber, 
       Terms(SELECT PaymentTerms 
               FROM PSD_customerPaymentTerms 
              WHERE PSD_customerPaymentTerms.PTId = NewInvoice_1.Terms
             ) AS Terms, 
       InvoiceDate, 
       OurQuote, 
       SalesPerson(SELECT FirstName 
                     FROM Employee 
                    WHERE Employee.EmployeeId = NewInvoice_1.SalesPerson
             ) AS SalesPerson, 
       CustomerName(SELECT CustomerName 
                      FROM Customer 
                     WHERE Customer.CustomerId = NewInvoice_1.CustomerName
             ) AS CustomerName, 
       OrderNumber, 
       GrandTotal, 
       (SELECT SUM(PaymentAmount) 
          FROM Payment_Receipt 
         WHERE Payment_Receipt.InvoiceNumber=NewInvoice_1.InvoiceNumber
             ) AS AmountPaid, 
       GrandTotal-IIf(AmountPaid Is Null,0,AmountPaid) AS AmountDue, 
       (SELECT InvoiceStatus 
          FROM Payment_Receipt 
         WHERE Payment_Receipt.InvoiceNumber=NewInvoice_1.InvoiceNumber
             ) AS Status -- Error getting after adding this line.
          FROM NewInvoice_1;

Payment_Receipt表包含ID,发票号,客户名称,总付款,余额,付款日期,付款金额,付款方式,付款备注,InvoiceStatus。

这是我的表Payment Table

如何从此表中获取InvoiceStatus?

1 个答案:

答案 0 :(得分:2)

解决此问题的一种常用方法是强制子查询使用列上的max()返回一行:

select max(someColumn)
from someTable
where ...

如果您的数据包含where子句的多行。

虽然这种方法可以使您的查询正常工作,但它可能无法提供您想要的结果。 where子句更有可能需要工作。也就是说,它在诊断问题时非常有用,特别是如果您不确定哪个子查询导致问题,您可以一次删除更改一个子查询。