机房收费系统的组合查询涉及到多个字段、多个操作符、多个查询内容的一种查询方式,另外还涉及到组合关系,那么我们怎么把他们联系起来呢?
下面我以查询学生上机信息为例,来梳理一下我的思考过程:
文章最后会给出源码。
这个功能实现出来后,我先分析了一下,这个功能实现的难点在SQL语句上而SQL语句的难点又在多个字段、多个操作符上面。那么好了问题的根源找到了。解决起来也就方便多了。
一、先假设
不是字段多、操作符多吗?让我先假设只有一个字段、一个操作符进行查询
查询Line_Info表中字段为cardno=1的数据
TxtSQL = "select * from Line_Info where cardno='1'"
二、在写出查询多个字段、多个操作符、两个与或关系的SQL语句
"select*from Line_Info where cardno='1' or studentNo > '2' and studentName='1'"诶!你有没有发现离目标又进一步了。
三、将多字段查询和SQL语句联系起来
上句SQL语句中的三个字段、三个操作符、两个与或关系、三个查询内容。分别用四个变量代替,赋值字段变量为哪个字段就查询哪个字段、赋值操作符变量为哪个操作符就用哪个操作符查询、赋值哪个与或关系就用哪个与或关系查询。至此问题迎刃而解了。
下面是我的源代码。
Dim Mrc As ADODB.Recordset
Dim TxtSQL As String '定义查询变量
Dim MsgTxt As String
Dim strfieldA As String
Dim strfieldB As String '定义字段变量
Dim strfieldC As String
Dim strOperatorA As String
Dim strOperatorB As String '定义操作符变量
Dim strOperatorC As String
Dim strValueA As String
Dim strValueB As String '定义查询内容变量
Dim strValueC As String
Dim strRelationA As String
Dim strRelationB As String '定义与或关系变量
If CmoField(0).Text = "" Then
strfieldA = "1" '下面的几个IF语句就是为变量赋值
'各种字段、各种操作符和与或关系的
Else
strfieldA = FieldName(CmoField(0).Text)
End If
If CmoField(1).Text = "" Then
strfieldB = "1"
Else
strfieldB = FieldName(CmoField(1).Text)
End If
If CmoField(2).Text = "" Then
strfieldC = "1"
Else
strfieldC = FieldName(CmoField(2).Text)
End If
If CmoOperator(0).Text = "" Then
strOperatorA = "="
Else
strOperatorA = FieldName(CmoOperator(0).Text)
End If
If CmoOperator(1).Text = "" Then
strOperatorB = "="
Else
strOperatorB = FieldName(CmoOperator(1).Text)
End If
If CmoOperator(2).Text = "" Then
strOperatorC = "="
Else
strOperatorC = FieldName(CmoOperator(2).Text)
End If
If TxtContent(0).Text = "" Then
strValueA = "1"
Else
strValueA = Trim(TxtContent(0).Text)
End If
If TxtContent(1).Text = "" Then
strValueB = "1"
Else
strValueB = Trim(TxtContent(1).Text)
End If
If TxtContent(2).Text = "" Then
strValueC = "1"
Else
strValueC = Trim(TxtContent(2).Text)
End If
If CmoCombinationRelation(0).Text = "" Then
strRelationA = "and"
Else
strRelationA = GetOrAnd(CmoCombinationRelation(0).Text)
End If
If CmoCombinationRelation(1).Text = "" Then
strRelationB = "and"
Else
strRelationB = GetOrAnd(CmoCombinationRelation(1).Text)
End If
If CmoField(0).Text = "" And CmoField(1).Text = "" And CmoField(2).Text = "" Then
MsgBox "请选择字段名!", vbOKOnly + vbExclamation, "提示"
Exit Sub
Else
If CmoOperator(0).Text = "" And CmoOperator(0).Text = "" And CmoOperator(0).Text = "" Then
MsgBox "请选择操作符!", vbOKOnly + vbExclamation, "提示"
Exit Sub
Else
If TxtContent(0).Text = "" And TxtContent(0).Text = "" And TxtContent(0).Text = "" Then
MsgBox "请输入查询内容!", vbOKOnly + vbExclamation, "提示"
Exit Sub
End If
End If
End If
TxtSQL = "select * from Line_Info where "
TxtSQL = TxtSQL & strfieldA & strOperatorA & "'" & strValueA & "'" & " " & strRelationA & " " & strfieldB & strOperatorB & "'" & strValueB & "'" & " " & strRelationB & " " & strfieldC & strOperatorC & "'" & strValueC & "'order by cardno"
Set Mrc = ExecuteSQL(TxtSQL, MsgTxt)
If Mrc.EOF And Mrc.BOF Then
MSFlexGrid1.Clear
MsgBox "数据库中没有符合此条件的记录!", vbOKOnly + vbExclamation, "提示"
Exit Sub
Mrc.Close
Else
MSFlexGrid1.Rows = 1
Do While Not Mrc.EOF
With MSFlexGrid1
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = Mrc.Fields(1)
.TextMatrix(.Rows - 1, 1) = Mrc.Fields(3)
.TextMatrix(.Rows - 1, 2) = Mrc.Fields(6)
.TextMatrix(.Rows - 1, 3) = Mrc.Fields(7)
.TextMatrix(.Rows - 1, 4) = Mrc.Fields(8)
.TextMatrix(.Rows - 1, 5) = Mrc.Fields(9)
.TextMatrix(.Rows - 1, 6) = Mrc.Fields(11)
.TextMatrix(.Rows - 1, 7) = Mrc.Fields(12)
.TextMatrix(.Rows - 1, 8) = Mrc.Fields(13)
Mrc.MoveNext
End With
Loop
Mrc.Close
End If
四、梳理自己的思维过程
首先遇到问题不能胆怯,在复杂的事情都是有一件件简简单单的事情组成的。遇到复杂的问题先从简单的开始,在逐步上升到复杂的层面。还有做事情要有条理性。只有这样想了,世界就是简单的。