有没有一种方法可以更有效地设置此代码?我试图通过几个不同的标准进行过滤,但我什至编译代码都遇到困难。另外,所有 IS NULL 的原因是我想让它在没有输入任何信息的情况下只接受所有数据。
有人对如何优化这段代码有任何建议吗?特别是 WHERE 部分。是否有我可以使用的 if then 语句?还是索引?
SELECT [Table Material Label].Serial, [Table Material Label].[Date Recieved], [Table Material Label].MaterialDescription, [Table Material Label].MaterialCode, [Table Material Label].Supplier,
[Table Material Label].[Lot Number], [Table Material Label].Weight, [Table Material Label].Quantity, [Table Material Label].[Purchase Order Number], [Table Material Label].[Received By], [Table Material Label].[Checked in By], [Table Material Label].[Total Weight]
FROM [Table Material Label]
WHERE (([Table Material Label].[Date Recieved])>=[Forms]![Report Generator]![Text6]
AND ([Table Material Label].[Date Recieved])<=[Forms]![Report Generator]![Text7])
AND ([Table Material Label].MaterialDescription = [Forms]![Report Generator]![repMaterial]
OR [Forms]![Report Generator]![repMaterial] IS NULL)
AND ([Table Material Label].MaterialCode = [Forms]![Report Generator]![repItem]
OR [Forms]![Report Generator]![repItem] IS NULL)
AND ([Table Material Label].[Lot Number] = [Forms]![Report Generator]![repLot]
OR [Forms]![Report Generator]![repLot] IS NULL)
AND ([Table Material Label].Weight = [Forms]![Report Generator]![repWeight]
OR [Forms]![Report Generator]![repWeight] IS NULL)
AND ([Table Material Label].Quantity = [Forms]![Report Generator]![repQuantity]
OR [Forms]![Report Generator]![RepQuantity] IS NULL)
AND ([Table Material Label].[Purchase Order Number] = [Forms]![Report Generator]![repPurchaseOrder]
OR [Forms]![Report Generator]![repPurchaseOrder] IS NULL)
AND ([Table Material Label].[Received By] = [Forms]![Report Generator]![repRecBy]
OR [Forms]![Report Generator]![repRecBy] IS NULL)
AND ([Table Material Label].[Checked in By] = [Forms]![Report Generator]![repCheckBy]
OR [Forms]![Report Generator]![repCheckBy] IS NULL)
AND ([Table Material Label].[Total Weight] = [Forms]![Report Generator]![repTotalWeight]
OR [Forms]![Report Generator]![repTotalWeight] IS NULL)
AND ([Table Material Label].Supplier = [Forms]![Report Generator]![Supp]
OR [Forms]![Report Generator]![Supp] IS NULL)
ORDER BY [Table Material Label].[Date Recieved], [Table Material Label].MaterialDescription, [Table Material Label].MaterialCode, [Table Material Label].Supplier;
最佳答案
您的 WHERE
代码最简单,至少我不知道任何更简单的解决方案。每个条件有两个条件,如果您需要显示空条件的所有记录,这是最低限度。此代码只有一个问题 - 您应该将查询保留为 SQL 文本格式。如果你切换到查询生成器,它会变得一团糟,几乎无法阅读。由于 SQL 文本并不复杂,因此运行此查询不会有任何问题,它不会被编译。当然,您应该为 WHERE
子句中的每个字段建立索引,以获得更好的性能。
关于mysql - Microsoft Access 的 SQL 代码效率和编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45222393/