【发布时间】:2021-05-01 20:58:36
【问题描述】:
我没有编程或数据库背景(最终用户除外),我正在使用 365 中的 Access 构建数据库。我对 SQL 的掌握非常有限。
我编写了一个多表查询来作为报告的基础,而且它大部分都有效。它将返回除一个表(CostumeInventory) 之外的所有请求的所有数据。对于该表,它会创建列(ItemName, Description, Condition, RentalRate, ReplacementValue),但这些列中的行是空白的。 。在 CostumeInventory 表中,它们是必需的,设置为短文本,并且都包含数据。
在查询设计中,如果我单击加入 CostumeInventory,我选择了选项 3。 1 什么都不返回,2 告诉我在尝试运行它时修复“模糊外连接”错误。我尝试以两种不同的方式创建子查询(一种加入 RentalInvenory 和 CostumeInventory,另一种将所有 Rental-- 表加入 CostumeInventory),但都没有产生任何结果。我可以尝试任何建议吗?先感谢您!下面是 Access 写的代码:
SELECT RentalClients.CompanyName, RentalDetails.ShowTitle, RentalDetails.Designer,
RentalDetails.OpeningDate, RentalDetails.ClosingDate, RentalDetails.FirstFitArrivalDate,
RentalDetails.ApprovalDate, RentalDetails.ReturnDate, RentalClients.CostumeContact,
RentalClients.CostumeEmail,
RentalInventory.InventoryID, CostumeInventory.ItemName, CostumeInventory.Description,
CostumeInventory.Condition, CostumeInventory.RentalRate, CostumeInventory.ReplacementValue
FROM (RentalClients
INNER JOIN RentalDetails ON RentalClients.ClientID = RentalDetails.ClientID)
INNER JOIN (CostumeInventory
RIGHT JOIN RentalInventory
ON CostumeInventory.InventoryRecordID = RentalInventory.InventoryID)
ON RentalDetails.RentalID = RentalInventory.RentalID
WHERE (((RentalDetails.RentalID)=[forms]![frmUpdateRental]![cboSelectRental]) AND
((RentalInventory.ReturnedApproval)=False) AND
((RentalInventory.ReturnedOpening)=False) AND
((RentalInventory.ReturnedFinal)=False));
【问题讨论】:
-
以
RentalInventory作为第一个表并左加入下一个(选项2) -
@Charlieface 那会这样写吗?:INNER JOIN (Rental Inventory LEFT JOIN CostumeInventory ON RentalInventory.InventoryID = CostumeInventory.InventoryRecordID
-
不应该去
FROM RentalInventory LEFT JOIN CostumeInventory INNER JOIN RentalDetails ON ... INNER JOIN RentalClients ON ... ON...,即其他表应该在LEFT JOIN CostumeInventory和ONcondition之间 -
谢谢,成功了!我还修复了一个数据类型不匹配的问题(为什么之前尝试左连接对我不起作用)。现在效果很好,干杯!