我已经搜索并搜索了这个问题的答案,但我找不到。我有一个选中的列表框,用户将在其中选择他们在计算机上遇到的问题。在选中的列表框中,您将看到诸如... 慢、病毒、坏硬盘... 并且根据他们选择的内容,我将告诉他们估计的维修费用。目前这是我构建查询的方式:
Dim mIssues As String = ""
For i = 0 To lstIssues.CheckedItemsCount - 1
If mIssues = "" Then
mIssues = String.Format("IssueName = '{0}'", lstIssues.CheckedItems(i))
Else
mIssues = String.Format("{0} OR IssueName = '{1}'", mIssues, lstIssues.CheckedItems(i))
End If
Next
上面的代码将查看他们选择了多少问题。如果他们只选择一个问题,那么它将返回如下字符串:IssueName = 'Whatever they selected'。如果他们选择了多个问题,它将返回如下字符串:IssueName = 'Whatever they selected' OR IssueName = 'The second selection'。所以基本上,如果他们选择了多个问题,我将在所有选择之间附加一个 OR。我这样做是为了在查询中动态构建 where 子句。
这是我的查询:
Dim mySQL As String = "SELECT IssueID, IssueTypeID, IssueName, IssueDescription, " _
& "CustomerID, IndividualCost, GroupCost, Active, ChargeType " _
& "FROM (SELECT IssueID, IssueTypeID, IssueName, IssueDescription, " _
& "CustomerID, IndividualCost, GroupCost, Active, ChargeType " _
& "FROM(cfg_Issues) " _
& "WHERE " & mIssues & " " _
& "GROUP BY IssueID, IssueTypeID, IssueName, IssueDescription, CustomerID, " _
& "IndividualCost, GroupCost, Active) " _
& "ORDER BY IndividualCost DESC, GroupCost ASC;"
如您所见,我的 where 子句来自第一段代码。我的问题是这个,有没有更好的方法来做到这一点???我知道必须有更好的方法来构建动态 where 子句查询,我想看看如何。感谢您提供任何可以帮助我的指导。