如果不为空,SQL 将在 Where 中包含条件
Posted
技术标签:
【中文标题】如果不为空,SQL 将在 Where 中包含条件【英文标题】:SQL to include condition in Where if not null 【发布时间】:2017-06-20 13:28:41 【问题描述】:在访问中,我一直在尝试对子表单中显示的内容设置用户过滤器,该子表单是检查列表。我尝试研究的其他方法没有奏效,但我用下面的代码成功了,如果用户填写任何其他过滤器选项,它将过滤。我能弄清楚的是如何让它接受多个过滤器,除非我拼出所有可能的框组合
那么无论如何这实际上是可能的还是我需要查看其他选项?
WHERE [STATUS] = "OPEN"
AND
(ANY FORM FILTERS is not Null [Filter by all those that are not null to its matching column])
如果表单过滤器不为空,是否有任何简单的方法来包含条件
SELECT Inspections.INS_ID
,Inspections.Category
,Inspections.Assigned_Officer
,Inspections.Raised_For
,Inspections.Account
,Inspections.Number
,Inspections.Street
,Inspections.Area
,Inspections.Postcode
,Inspections.Date_Raised
,Inspections.Reason
,Inspections.INS_Comments
FROM Inspections
WHERE (
((Inspections.STATUS) = "Open")
AND (([Forms]![Manage_Open]![Filter_ID]) IS NULL)
AND (([Forms]![Manage_Open]![Filter_account]) IS NULL)
AND (([Forms]![Manage_Open]![Filter_officer]) IS NULL)
AND (([Forms]![Manage_Open]![Filter_Number]) IS NULL)
AND (([Forms]![Manage_Open]![Filter_Postcode]) IS NULL)
AND (([Forms]![Manage_Open]![Filter_Category]) IS NULL)
AND (
(([Forms]![Manage_Open]![Filter_From]) IS NULL)
AND (([Forms]![Manage_Open]![Filter_To]) IS NULL)
)
)
OR (
([Forms]![Manage_Open]![Filter_ID]) IS NOT NULL
AND ([Forms]![Manage_Open]![Filter_ID]) = [Inspections].[INS_ID]
)
OR (
([Forms]![Manage_Open]![Filter_account]) IS NOT NULL
AND ([Forms]![Manage_Open]![Filter_account]) = [Inspections].[Account]
)
OR (
([Forms]![Manage_Open]![Filter_officer]) IS NOT NULL
AND ([Forms]![Manage_Open]![Filter_officer]) = [Inspections].[Assigned_Officer]
)
OR (
([Forms]![Manage_Open]![Filter_Number]) IS NOT NULL
AND ([Forms]![Manage_Open]![Filter_Number]) = [Inspections].[Number]
)
OR (
([Forms]![Manage_Open]![Filter_Postcode]) IS NOT NULL
AND ([Forms]![Manage_Open]![Filter_Postcode]) = [Inspections].[Postcode]
)
OR (
([Forms]![Manage_Open]![Filter_Category]) IS NOT NULL
AND ([Forms]![Manage_Open]![Filter_Category]) = [Inspections].[Category]
)
OR (
(
(([Forms]![Manage_Open]![Filter_From]) IS NOT NULL)
AND (([Forms]![Manage_Open]![Filter_To]) IS NOT NULL)
)
AND ([Inspections].[Raised_For]) BETWEEN (
([Forms]![Manage_Open]![Filter_From])
AND ([Forms]![Manage_Open]![Filter_to])
) )
);
【问题讨论】:
【参考方案1】:如果我理解正确,您有一个包含多个未绑定控件的表单,用户将填写这些控件。然后您想根据用户输入的内容过滤您的子表单。
在这种情况下,您尝试使用的方法将变得非常复杂且难以维护,更不用说数据量大时速度慢了。
最好的办法是在代码中构建过滤器。
我经常做的是将控件命名为与字段名称相同,并将字段类型放在控件的标记属性中。然后循环通过控件来构建过滤器。这是空气代码,但应该给你一个开始。
Dim strFilter as string
Dim ctL as Access.control
For Each ctL in Me.Controls
If Not ISNull(ctL) And ctL.Tag <> "" Then
strFilter = strFilter & " AND " & ctl.Name & " = "
If ctL.Tag = "Text" Then
strFilter = strFilter & "'" & Replace(ctl, "'","''") & "'"
ElseIf ctL.Tag = "Date" Then
strFilter = strFilter & "#" & ctl & "#"
Else
strFilter = strFilter & ctlEnd If
Next
If strFilter <> "" Then
strFilter = mid(strFilter, 6)
strFilter = " Where " & strFilter
End If
Me.yourSubForm.Form.Recordsource = "Select * From <yourQuery> " & strfilter
【讨论】:
好吧,我大部分时间都在工作,我已经了解了它是如何工作的,但我仍然没有用这种方法过滤它,我认为我的问题是“把控件标签属性中的字段类型”我从来没有使用过标签,感觉很傻,但这是否意味着将文本、日期、自动编号等字段类型添加到每个过滤器框(控件)的 DATA 下的智能标签属性中? 另外,如果我不想从查询中提取信息,我可以将“Select * From以上是关于如果不为空,SQL 将在 Where 中包含条件的主要内容,如果未能解决你的问题,请参考以下文章
MyBatis框架中的条件查询!关键字exists用法的详细解析
oracle SQL查询中,如何在where中用条件语句,判断不同情况,追加不同的And条件?