Access 2010 SQL:多个条件,其中子句未正确过滤

时间:2015-05-13 19:49:54

标签: sql ms-access-2010 where-clause

我正在尝试为MS Access 2010编写SQL查询以选择6个字段的任意组合。我已经将63种可能的组合写入了我的查询中,每种组合都被括起来并用OR分隔。在内部,每个都由AND连接。此外,每个字段被设置为从用户请求参数。

我遇到的问题是,如果我输入(示例)字段A,B和C的参数,我只返回A和B的过滤值,而查询显示不符合参数条件的项。

代码示例:

`SELECT Main.DISPATCHER, Main.DateCall, Main.Status, Main.[Pick Up 20], Main.[Drop Off 20], Main.Shift, Main.Day, Main.Time, Main.[Call Type], Main.[ETA Given], Main.ZONE, Main.Priority, Main.Comments, Main.DODept, Main.ID, Main.PU, Main.DO, Main.[Pick Up], Main.Caller, Main.DropOff
FROM Main
WHERE (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
       AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
       AND Main.Status=[Status: ACCEPTED or DECLINED?]
       AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
       AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
       AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List])
  OR (Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.Shift=[What shift?AM/PM]
      AND Main.DISPATCHER=[Which Dispatcher?Ex Justin])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR Main.DISPATCHER=[Which Dispatcher?Ex Justin]
  AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
  AND Main.Status=[Status: ACCEPTED or DECLINED?]
  AND Main.Shift=[What shift?AM/PM]
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex jUSTIN]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DISPATCHER=[Which Dispatcher?Ex Justin]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.Status=[Status: ACCEPTED or DECLINED?])
  OR (Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])
  OR (Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.Status=[Status: ACCEPTED or DECLINED?]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.[Pick Up 20]=[What Pick Up?Use Facility List])
  OR (Main.[Pick Up 20]=[What Pick Up?Use Facility List]
      AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.[Drop Off 20]=[What Drop Off?-Use Facility List]
      AND Main.Shift=[What shift?AM/PM])
  OR (Main.Shift=[What shift?AM/PM]);` 

非常感谢任何帮助/逻辑。感谢。

1 个答案:

答案 0 :(得分:0)

是的,这不是一种非常有效的搜索方式。我的意思是,这个:

OR (Main.Status=[Status: ACCEPTED or DECLINED?])
OR (Main.Status=[Status: ACCEPTED or DECLINED?]
  AND Main.[Pick Up 20]=[What Pick Up?Use Facility List]
  AND Main.[Drop Off 20]=[What Drop Off?-Use Facility List])

在逻辑上等同于此:

OR (Main.Status=[Status: ACCEPTED or DECLINED?])

然后在中间你有一堆AND语句。

[...]
OR Main.DISPATCHER=[Which Dispatcher?Ex Justin]
AND Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:]
AND Main.Status=[Status: ACCEPTED or DECLINED?]
AND Main.Shift=[What shift?AM/PM]
OR [...]

这基本上会覆盖你所拥有的其他一切。 所有必须满足这些条件。

我们假设当您未提交字段时,提示值为NULL。我们还假设您必须提交通话日期范围的两个日期。要进行完整搜索,您可以执行以下操作:

WHERE (Main.DISPATCHER=[Which Dispatcher?Ex Justin] OR [Which Dispatcher?Ex Justin] IS NULL)
    AND (Main.DateCall BETWEEN [Enter Start Date:] AND [Enter Ending Date:] OR [Enter Start Date:] IS NULL OR [Enter Ending Date:] IS NULL)
    AND (Main.Status=[Status: ACCEPTED or DECLINED?] OR [Status: ACCEPTED or DECLINED?] IS NULL)
    AND (Main.[Pick Up 20]=[What Pick Up?Use Facility List] OR [What Pick Up?Use Facility List] IS NULL)
    AND (Main.[Drop Off 20]=[What Drop Off?-Use Facility List] OR [What Drop Off?-Use Facility List] IS NULL)
    AND (Main.Shift=[What shift?AM/PM] OR [What shift?AM/PM] IS NULL)