SQL代码效率&编译Microsoft Access的问题

时间:2017-07-20 18:18:49

标签: mysql ms-access

有没有办法以更有效的方式设置此代码?我试图过滤几个不同的标准,我甚至很难编译代码。所有IS NULL的原因还在于我想要这样做,如果没有输入信息,它只接受所有数据。

任何人都有关于如何优化此代码的任何提示?特别是 WHERE 部分。是否有我可以使用的if语句?还是索引?

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;

1 个答案:

答案 0 :(得分:0)

WHERE的代码最简单,至少我不知道任何更简单的解决方案。每个条件有两个条件,如果您需要显示空条件的所有记录,这是最小的。此代码只有一个问题 - 您应该以SQL文本格式保留查询。如果切换到查询构建器,它将使整个混乱,几乎不可读。作为SQL文本,它并不复杂,运行此查询不应该有任何问题,它不会被编译。当然,您应该在WHERE子句中为每个字段设置索引,以获得更好的性能。