【问题标题】:SQL code efficiency & compiling issues with Microsoft AccessMicrosoft Access 的 SQL 代码效率和编译问题
【发布时间】:2017-07-20 18:18:48
【问题描述】:

有没有办法以更有效的方式设置此代码?我试图过滤几个不同的标准,我什至很难编译代码。此外,所有 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;

【问题讨论】:

  • 对于所有 or 的考虑:AND ([Table Material Label].MaterialDescription = Nz([Forms]![Report Generator]![repMaterial], [Table Material Label].MaterialDescription) 从而消除所有 ors...不确定它是否有助于不进行测试。如果形式 repMaterial 为空,则基本使用材料描述代替,本质上 1=1 并且可以省略 or。
  • xQbert,这非常好用。谢谢。更新主帖。

标签: mysql ms-access


【解决方案1】:

WHERE 的代码最简单,至少我不知道任何更简单的解决方案。每个条件都有两个条件,如果您需要显示空条件的所有记录,这是最低要求。这段代码只有一个问题——您应该将查询保留为 SQL 文本格式。如果您切换到查询生成器,它将变得一团糟,几乎无法阅读。由于 SQL 文本并不复杂,运行此查询应该没有任何问题,它不会被编译。当然,您应该为WHERE 子句中的每个字段设置索引以获得更好的性能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 2012-05-07
    • 2017-01-28
    • 1970-01-01
    • 2011-07-07
    相关资源
    最近更新 更多