【问题标题】:MS Access Database SQL QueryMS Access 数据库 SQL 查询
【发布时间】:2014-02-01 10:42:03
【问题描述】:

我有 3 个表,分别称为发票、客户和公司。我想使用 Query 将这 3 个表合并为一个表。在发票表中包含客户 ID 和公司 ID。如何加入 3 个表?

我尝试使用此查询很好地处理发票和客户表。但我不知道用这个添加第三张桌子。

SELECT RPT_Invoice_Less.InvoiceNumber, RPT_Invoice_Less.Terms,
    RPT_Invoice_Less.Invoicedate, RPT_Invoice_Less.OurQuote,
    RPT_Invoice_Less.SalesPerson, RPT_Customer.CustomerName,
    RPT_Customer.CustomerId, RPT_Customer.ContactPerson,
    RPT_Customer.BillingAddress, RPT_Customer.DeliveryAddress,
    RPT_Invoice_Less.OrderNumber, RPT_Invoice_Less.ShippingBy,
    RPT_Invoice_Less.ShipReferenceNo, RPT_Invoice_Less.Notes,
    RPT_Invoice_Less.Price, RPT_Invoice_Less.Discount,
    RPT_Invoice_Less.Shipping, RPT_Invoice_Less.Tax,
    RPT_Invoice_Less.GrandTotal, RPT_Invoice_Less.Company
FROM RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId;

此代码适用于 2 个表

SELECT RPT_Invoice_Less.InvoiceNumber, RPT_Invoice_Less.Terms, RPT_Invoice_Less.Invoicedate, RPT_Invoice_Less.OurQuote, RPT_Invoice_Less.SalesPerson, RPT_Customer.CustomerName, RPT_Customer.CustomerId, RPT_Customer.ContactPerson, RPT_Customer.BillingAddress, RPT_Customer.DeliveryAddress, RPT_Invoice_Less.OrderNumber, RPT_Invoice_Less.ShippingBy, RPT_Invoice_Less.ShipReferenceNo, RPT_Invoice_Less.Notes, RPT_Invoice_Less.Price, RPT_Invoice_Less.Discount, RPT_Invoice_Less.Shipping, RPT_Invoice_Less.Tax, RPT_Invoice_Less.GrandTotal, RPT_OrionSystem.Company, RPT_OrionSystem.CompanyId
FROM RPT_Invoice_Less 
INNER JOIN RPT_Customer 
ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId
INNER JOIN RPT_OrionSystem
ON RPT_Invoice_Less.CompanyId = RPT_OrionSystem.CompanyId;

此代码显示语法错误。

帮我添加第三个Company表。

【问题讨论】:

  • 要回答您的问题,我们需要知道 Company 表与其他两个表之一之间的关系在哪里。例如,RPT_Customer 表中的 CompanyID。

标签: c# mysql sql ms-access


【解决方案1】:

假设您在RPT_Customer 表或RPT_Invoice_Less 中有一个CompanyID 字段(或类似的字段),只需添加另一个INNER JOIN

....
FROM ((RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId)
INNER JOIN RPT_OrionSystem 
ON  RPT_Invoice_Less.CompanyID = RPT_OrionSystem.CompanyID)

【讨论】:

  • 嗨,我编辑了我的问题,现在我尝试了与您提到的相同但出现语法错误
  • 尝试添加一对括号
  • 在哪里添加?我已经尝试过在(RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId)(RPT_Customer.CompanyID = RPT_CompanyData.CompanyID) 上添加括号仍然是相同的语法错误
  • 回答更新刷新,去掉末尾的分号。你能在这个调用周围添加确切的 C# 代码吗?
  • 是的,添加括号后工作正常。这个查询我只在 MS Access 数据库中使用。然后我填充到 C#。非常感谢。
猜你喜欢
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多